java - 如何使用数组从 firebase 查询?

标签 java firebase firebase-realtime-database android-recyclerview

我的目标是从 user2 关注的 user1、user3、user4 获取帖子。

 //Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child
            ("Users").child("following").child(uid);

    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    //Get map of users in datasnapshot

                    collectDispayNames((Map<String, Object>) dataSnapshot.getValue());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    //handle databaseError
                }
            });




private void collectDispayNames(Map<String, Object> users) {

    displayNames = new ArrayList<>();
    //iterate through each following, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()) {

        //Get user map
        Map singleUser = (Map) entry.getValue();

        //Get following_id field and append to list
        displayNames.add((String) singleUser.get("who_i_follow"));

        //Toast.makeText(this, displayNames.toString(), Toast.LENGTH_LONG).show();

        int size = displayNames.size();
        if (size > 0){

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
        }else {

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents");
        }
        if (size >1){

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
        }else {
            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents");
        }

    }
}

现在假设我检索 user2 跟随到名为 showOwnerId 的数组中的所有用户

我可以获得user2关注者的数组,但由于它有很多人关注,如何将其关注的所有用户id插入数据库像这样查询

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));

最佳答案

无法一次性查询多个值。您必须为跟踪的每个用户运行单独的查询,并在客户端合并结果。

因为 Firebase 可以 pipeline these queries over a single connection ,性能并不像一些开发人员想象的那么糟糕。

关于java - 如何使用数组从 firebase 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571828/

相关文章:

ios - swift 和火力地堡 : Retrieving location data from Firebase to show on Google map marker

java - AWS Java API授权不一致

javascript - 如何将从 useEffect 中获取的初始值放入文本字​​段中?

java - 我是否在组合键中正确定义了多对一?

android - 升级到 androidX 后 Zip 文件 '...' 已经包含条目 'AndroidManifest.xml' ,构建时无法覆盖错误

javascript - 将 API Node.js 后端(Firebase)与 React.js 连接

android - Firebase 中的 ExceptionInInitializerError

Swift - Firebase 快照到变量中

Java RegEx 和换行符 - 错误或预期行为?

java - 接口(interface)和动态方法调度