Firebase嵌套onCompleteListeners的Firestore循环两次

时间:2018-08-13 07:57:19

标签: android firebase google-cloud-firestore

注释我正在使用firebasefirestore来存储有关用户的一些详细信息。我有两种不同的Firestore引用,一种是“ SavedRooms”,另一种是“ Rooms”。在“ SavedRooms”中,我仅存储了“ Room”的ID,以便当用户尝试检查已保存的房间时,它将首先调用saverooms参考,并使用ID和Call Rooms参考。但是问题在于嵌套的completelistener,即数据在列表中添加了两次。支持我在保存的参考中有两个文档。然后将四个数据添加到我的列表中。这是嵌套侦听器的问题吗?还是有其他替代方法?

        private void initRecyclerView() {

    ArrayList<Room> rooms = new ArrayList<>();
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();

    firebaseFirestore.collection("SavedRooms")
            .document(firebaseAuth.getCurrentUser().getUid())
            .collection("interested").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()){
                        for(QueryDocumentSnapshot d:task.getResult()){
                            firebaseFirestore.collection("Rooms")
                            .whereEqualTo("ReferenceId",d.getString("ReferenceId"))
                            .get()
                            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task1) {
                                if(task1.isSuccessful()){
                                    for(QueryDocumentSnapshot d1:task.getResult()){
                                        rooms.add(new Room(d1.getString("Address"),d1.getString("Price")
                                        d1.getString("Type")));
                                   }
                                   recyclerView.setAdapter(new CustomAdapter(rooms));
                                }
                            }
                        }
                    }else{
                        toast("Unable to Load Data");
                        toast(task.getException().getMessage());
                    }
                }
            });
}

0 个答案:

没有答案