使用Firebase - Android以双向关系检索用户

时间:2016-08-27 03:04:11

标签: java android json firebase firebase-realtime-database

我正试图找出一种检索跟随我的所有用户的方法。假设我是user1,user2和user3是我的粉丝。我怎样才能检索它们而不是检索所有用户并在客户端过滤它们?

这就是我的结构:

{
  "followers" : {
    "user1" : {
      "user2" : true,
      "user3" : true
    }
  },

  "following" : {
    "user2" : {
      "user1" : true
    },
    "user3" : {
      "user1" : true
    }
  },

  "users" : {
    "user1" : {
      "firstName" : "User One"
    },
    "user2" : {
      "firstName" : "User Two"
    },
    "user3" : {
      "firstName" : "User Three"
    }
  }
}

我还没有任何代码,因为我不知道如何开始。任何帮助表示赞赏。谢谢你的阅读。

编辑: 这就是我的工作方式。我不知道它是否是最好的选择,但它现在有效。

ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
                Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
                // A new comment has been added, add it to the displayed list
                String uid = dataSnapshot.getKey();

                mDatabase.child("users").child(uid).addListenerForSingleValueEvent(
                        new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                // Get user value
                                User user = dataSnapshot.getValue(User.class);
                                users.add(user);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                            }
                        });
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
                Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
                // A comment has changed, use the key to determine if we are displaying this
                // comment and if so displayed the changed comment.

                //Comment newComment = dataSnapshot.getValue(Comment.class);
                String commentKey = dataSnapshot.getKey();
                System.out.println("Changed: "+commentKey);
                notifyDataSetChanged();
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
                // A comment has changed, use the key to determine if we are displaying this
                // comment and if so remove it.

                String commentKey = dataSnapshot.getKey();
                System.out.println("Removed: "+commentKey);
                notifyDataSetChanged();
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
                Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
                // A comment has changed position, use the key to determine if we are
                // displaying this comment and if so move it.

                //User user = dataSnapshot.getValue(User.class);
                String commentKey = dataSnapshot.getKey();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w(TAG, "postComments:onCancelled", databaseError.toException());
                Toast.makeText(context, "Failed to retrieve followers.", Toast.LENGTH_SHORT).show();
            }
        };
        mDatabase.child("followers").child(currentUser.getUid()).addChildEventListener(childEventListener);

1 个答案:

答案 0 :(得分:1)

您可以在ref.child("followers").child(user.getUid())的一次阅读(甚至是查询)中获取关注者的用户ID。

然后,您确实需要循环以从/users/$followerId下检索每个关注者。这并不像您想象的那么昂贵,因为Firebase客户端有效地将请求传递给服务器。请参阅Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly