android - 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();
}
}
}
解决方案
Firebase 不保证这一点,除非您使用“orderBy”进行的查询强制执行它,并且还需要在那里指定排序/排序方向。
请在此处查看:使用 Firebase 订购和限制数据
因此,基本上以您想要的方式提取数据(这将节省 Android 中的处理。如果您无法在查询中完全实现它,那么您可能必须在 Android 代码(Java / Kotlin)中进行额外的过滤/排序。
如果您对此有任何疑问,请不要犹豫。谢谢。
推荐阅读
- java - 尝试访问 pojo 时出现 NoMessageBodyWriterFoundFailure
- azure - Azure - 带有正则表达式的 Web API 和路由模板
- python - 如何手动安装 Numpy (Linux)?
- css - CSS Grid 将主页分成两部分
- docker - traefik docker 标签中基于主机和路径的路由规则的混合
- html - 如何根据悬停效果设置背景颜色,即当鼠标悬停在为类的背景颜色定义的相同 css 定义中的 li 项目上时?
- python - 对一个列表进行排序以使两个列表具有正确的顺序对应关系
- knockout.js - Knockout.js foreach 没有给出错误并且没有显示结果
- python - 如何在 pyspark 列表达式中引用名称中带有连字符的列?
- c++ - 如何访问子类中的小部件?