Reputation: 3534
I'm trying to create a Firestore document consisting of a document whose keys are Web URLs and the values are a collection of documents. I specify the document reference using the URL and the user ID, as follows:
private String replaceSlashSeparators(String url) {
return url.substring(0, 8) + url.substring(8).replaceAll("/", "%2F");
}
private void postComment(String page_url, String userID, String subj, String msg) {
DocumentReference mLinkDocRef = db.collection("links").document(replaceSlashSeparators(page_url)).collection("comments").document(userID);
Map<String, Object> data = new HashMap<>();
data.put(FSNAME, mUsername);
data.put(FSIMGURL, mPhotoUrl);
data.put(FSDEVICEID, deviceID);
data.put(FSSUBJECT, subj);
data.put(FSCOMMENT, msg);
mLinkDocRef.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.e(TAG, "Listen failed.", task.getException());
crashlytics.log("Listen failed." + Objects.requireNonNull(task.getException()).getMessage());
return;
}
showCommentPostedDialog();
});
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
Toast.makeText(context, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
I'm expecting to see a document in the "links" collection containing a URL and the "comments" document having the data described above, but instead no document is created. The set() calls onSuccess(), so the document should have been created. Anyone have an idea why this is happening?
Upvotes: 0
Views: 225
Reputation: 3534
This is embarassing. It turned out my code was correct. The reason the document didn't appear was because I had to refresh the collection. I never used to have to do that to get the document to appear, but I guess that when you have a longish hierarchy, it's necessary.
Upvotes: 1