Android Firestore多次编写同一文档并停止工作一段时间

Android Firestore多次编写同一文档并停止工作一段时间,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我正在尝试向我的应用程序添加一个审阅功能,因此我将此代码添加到我的活动中 firebaseFirestore.collection("Rating").document(orderId).addSnapshotListener(new EventListener<DocumentSnapshot>() { @Override public void onEvent(DocumentSnapshot doc

我正在尝试向我的应用程序添加一个审阅功能,因此我将此代码添加到我的活动中

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();
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
firebaseFirestore.collection(“Rating”).document(orderId).addSnapshotListener(新的EventListener()){
@凌驾
public void onEvent(DocumentSnapshot DocumentSnapshot,FireBaseFireStoree异常){
if(documentSnapshot.exists()){
float currate=(documentSnapshot.getLong(“评级”)+ratingBar.getRating())/2;
firebaseFirestore.collection(“Rating”).document(orderId).update(“Rating”,currate).addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
final-Map postmap=新HashMap();
postmap.put(“rating”,ratingBar.getRating());
postmap.put(“反馈”,提要);
firebaseFirestore.collection(“ratings”).document(orderId).collection(“ratings”).add(postMaps).addOnCompleteListener(新OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
完成();
}
});
}
});
}
否则{
final Map postMap=新HashMap();
postMap.put(“rating”,ratingBar.getRating());
邮戳放置(“反馈”,提要);
firebaseFirestore.collection(“Rating”).document(orderId).set(postMap).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
firebaseFirestore.collection(“ratings”).document(orderId).collection(“ratings”).add(postMap).addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
完成();
}
});
}
});
}
}
});

当习惯了第一次。它一次创建了大约20个文档,而本应只创建一个文档。之后,当我重新运行应用程序时,它就停止了数据写入。我试着查看代码,但一切似乎都很好,这是我经常使用的东西。任何帮助都将不胜感激

关键是您正在将文档添加到同一集合中,因此它会检测到更改并再次调用快照侦听器,您应该使用另一个集合或使用另一个系统来获取文档。查看firestore文档:

关键是您要将文档添加到同一集合中,以便它检测到更改并再次调用快照侦听器,您应该使用另一个集合或使用另一个系统来获取文档。查看firestore文档:

根据您的代码,我看到您在一个集合中侦听文档以获取更改,在该侦听器中,您也在更新同一文档,从而创建一个无止境的循环。根据您的代码,我看到您在一个集合中侦听文档以获取更改,在该侦听器中,您也在更新同一文档,从而创建一个无止境的循环。