我正在开发应用程序,我只想显示其联系电话保存在手机中的用户的数据。因此,我检索了一个联系电话列表 contactList
。现在,我想从 Firebase 获取所有 contactList
用户的帖子。有什么方法可以让 Firebase 只询问我的 contactList
中的那些节点。一种方法是检索所有用户,然后从中获取相关用户(就像我在下面所做的那样)。有更好的方法吗?
Users: {
7828272892 : {
name: xyz,
gender: male,
phoneNo: 7828272892
Posts: {
SomeKey1: {
content: "This is post 1", Var2: "kkk"} }},
7924272894 : {name: abc, gender: male, phoneNo: 7924272894}
}
代码:
databaseReference.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
List<Post> allItems = new ArrayList<Post>();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
if (contactList.contains(postSnapshot.child("phoneNo").getValue())) {
// retrieve posts
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
最佳答案
Is there any way I can ask the firebase for only those nodes in my
contactList
?
是的,您已经采用的方式。
Is there any better way to do so?
执行此操作的方式是 Firebase 中使用的常见做法。您查询数据库并检查数据是否已存在于列表中,在您的例子中是 contactList
。如果列表包含 phoneNo
,那么您可以检索这些帖子。
关于java - 从 Firebase 检索多个子节点的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50984410/