Ad
Firestore Writing Same Document Multiple Times And Stops Working For A While
I Am trying to add a review feature to my app hence I added this code to my activity.
firebaseFirestore.collection("Rating").document(orderId).addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if(documentSnapshot.exists()){
float currate=(documentSnapshot.getLong("rating")+ratingBar.getRating())/2;
firebaseFirestore.collection("Rating").document(orderId).update("rating",currate).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
final Map<String,Object> postMaps=new HashMap<>();
postMaps.put("rating",ratingBar.getRating());
postMaps.put("feedback",feed);
firebaseFirestore.collection("Rating").document(orderId).collection("ratings").add(postMaps).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
finish();
}
});
}
});
}
else {
final Map<String,Object> postMap=new HashMap<>();
postMap.put("rating",ratingBar.getRating());
postMap.put("feedback",feed);
firebaseFirestore.collection("Rating").document(orderId).set(postMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
firebaseFirestore.collection("Rating").document(orderId).collection("ratings").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
finish();
}
});
}
});
}
}
});
when used to first time. It created about 20 documents at once when only one was supposed to be created. And after that it just stopped writing data when I re run the app. I tried looking over the code but everything seemed fine and its something which I use regularly. Any help would be appreciated
Ad
Answer
The point is that you are adding the document to the same collection so it detects a change and calls the Snapshot listener again, you should use another collection or use another system to get the documents. Check firestore documentation: Firestore documentation
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad