【问题标题】:Cloud Firestore - Get Documents From Multiple LocationsCloud Firestore - 从多个位置获取文档
【发布时间】:2018-03-17 00:56:03
【问题描述】:

当用户在我的应用中查看他的好友列表时,我希望应用遍历列表中的每个用户并从Cloud Firestore 检索他的最新信息。

这是我当前的代码:

 final CollectionReference usersRef= FirebaseFirestore.getInstance().collection("users");

            usersRef.document(loggedEmail).collection("friends_list").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                    if (!documentSnapshots.isEmpty()){


                        for (DocumentSnapshot friendDocument: documentSnapshots) {

                            usersRef.document(friendDocument.getString("email")).get().addOnSuccessListener
                                    (new OnSuccessListener<DocumentSnapshot>() {
                                @Override
                                public void onSuccess(DocumentSnapshot documentSnapshot) {
                                    User friend=documentSnapshot.toObject(User.class);
                                friendsList_UserList.add(friend);

                                }
                            });

                        }


                        ///...

                    }

                    else
                        noFriendsFound();

                }

这是我想要的过程的说明:

如您所见,我可以通过这种方式获取每个用户的信息,但是我无法找到监听这个过程的方法,并在我获得了所有朋友的信息后继续用户列表。

我可以一次获得所有朋友信息的方法吗?

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    Firestore 不直接支持您要求的联接。

    您可以使用QuerySnapshot 中的getDocumentChanges 构造一个链式侦听器,以跟踪您应该听哪些朋友。

    想象一下,如果你保存一张这样的朋友听众注册地图

    Map<String, ListenerRegistration> friendListeners = new HashMap<>();
    

    然后你可以这样注册:

    usersRef.document(loggedEmail).collection("friends_list")
        .addSnapshotListener(new EventListener<QuerySnapshot>() {
          @Override
          public void onEvent(QuerySnapshot snapshot, FirebaseFirestoreException error) {
            for (DocumentChange change : snapshot.getDocumentChanges()) {
              DocumentSnapshot friend = change.getDocument();
              String friendId = friend.getId();
              ListenerRegistration registration;
              switch (change.getType()) {
                case ADDED:
                case MODIFIED:
                  if (!friendListeners.containsKey(friendId)) {
                    registration = usersRef.document(friendId).addSnapshotListener(null);
                    friendListeners.put(friendId, registration);
                  }
                  break;
    
                case REMOVED:
                  registration = friendListeners.get(friendId);
                  if (registration != null) {
                    registration.remove();
                    friendListeners.remove(friendId);
                  }
                  break;
              }
            }
          }
        });
    

    但请注意,这实际上可能不是一个好主意。您最好将足够的信息下推到 friends_list 文档中,这样您只需在真正深入了解该朋友的详细信息后才需要加载实际的朋友用户文档。

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2021-05-24
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多