Ad

Firestore Writing Same Document Multiple Times And Stops Working For A While

- 1 answer

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
Ad