首页 > 解决方案 > Firebase 是否总是按顺序返回提交的数据?

问题描述

我正在尝试显示各种用户(例如 tinder)的用户资料,但我想知道 firebase 是否总是向其他新用户显示最先注册的用户,或者迭代是随机的,这将保证结果是随机的,每个人都有机会得到显示是平等的?如果没有,有人知道如何在android中让它随机吗?我是怎么做的

FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);

编辑这是我认为排序无济于事的代码,因为我希望它是随机的

 if(mAuth.getCurrentUser().isEmailVerified())
    {
        FirebaseDatabase.getInstance().getReference().child("Users").child(UserSex).child(currentUserID).child("isVerified").setValue(true);
    }
    DatabaseReference oppositeSexDB= FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);
    System.out.println(OppositeUserSex + "oppositeUser");
    oppositeSexDB.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull @NotNull DataSnapshot snapshot, @Nullable @org.jetbrains.annotations.Nullable String previousChildName) {
            if(snapshot.exists())
            {
                if(!snapshot.child("Selection").child("Nah").hasChild(currentUserID)) {
                    if(snapshot.child("Url").exists() && snapshot.child("isVerified").exists()) {    //If user has image only then show them else dont display them && if they are verified
                        items.add(new ItemModel(snapshot.child("Url").getValue().toString(), snapshot.child("Name").getValue().toString(), "", "", snapshot.getKey()));
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }

标签: androidfirebasefirebase-realtime-database

解决方案


Firebase 不保证这一点,除非您使用“orderBy”进行的查询强制执行它,并且还需要在那里指定排序/排序方向。

请在此处查看:使用 Firebase 订购和限制数据

因此,基本上以您想要的方式提取数据(这将节省 Android 中的处理。如果您无法在查询中完全实现它,那么您可能必须在 Android 代码(Java / Kotlin)中进行额外的过滤/排序。

如果您对此有任何疑问,请不要犹豫。谢谢。


推荐阅读