As you can see in the image, whenever I try to update(not replace) a field with a new value, it duplicates the data, and adds up on top of the duplicate. What is causing this issue?
Whenever a user posts, I need to add the post id to an existing array of post ids.
hierarchy:
users - collection - document(emblem(String), photo(String), postIds(String[])...)
mDb = FirebaseFirestore.getInstance();
// remove old ids
Map<String, Object> oldIds = new HashMap<>();
oldIds.put("posts", _oldPostList);
// add new ids
Map<String, Object> newIds = new HashMap<>();
_oldPostList.add(_newValue);
newIds.put("posts", _oldPostList);
String docId = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = mDb.collection("users").document(docId);
docRef.update("posts", FieldValue.arrayRemove(oldIds)).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
docRef.update("posts", FieldValue.arrayUnion(newIds)).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
sendBroadcast(_context, _action, true, null);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
sendBroadcast(_context, _action, false, null);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
sendBroadcast(_context, _action, false, null);
}
});
CodePudding user response: