java - 使用 Firebase 检索双向关系中的用户 - Android

标签 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);

最佳答案

您可以从 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 .

关于java - 使用 Firebase 检索双向关系中的用户 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39177059/

相关文章:

android - 当在加权 LinearLayout 中显示时,RatingBar 被水平截断

android - RecyclerView 的 RealmResults

java - 单击从服务器获取数据时如何设置进度条?

python - Django REST 框架 : Flatten nested JSON to many objects

java - SQLContext.read() Spark 中的 NullPointerException

java - 获取素数总数

java - Android 中的本地时间

java - 如何知道单元格数据没有被更改或修改?

java - 如何从 Native SQL 读取具有多个字段的 SQL

php - 从 Laravel Link 加载 Webix 数据