gpt4 book ai didi

android - 从 Firebase 查询具有在 ArrayList 中找到的子值的对象

转载 作者:行者123 更新时间:2023-11-29 00:54:19 25 4
gpt4 key购买 nike

我正在尝试将节点从 Firebase 实时数据库查询到我的 firebase 回收器适配器,其中在字符串 ArrayList 中找到“userId”子值。

所以我有一个名为“Follows”的节点,其中存储了应用程序内关注的每个用户。 It looks like this .然后我有一个名为“帖子”的节点,其中存储了任何用户的每个帖子。每个单独的帖子都有多个子项,其中包含有关该帖子的信息,其中一个是名为“userId”的子项。 Looking like this .

我已经检索了每个如下的用户 ID:

final ArrayList<String> usersFollows = new ArrayList<>();
DatabaseReference usersFollowsRef = FirebaseDatabase.getInstance().getReference().
child("Follows").child(currentUserId);

usersFollowsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
String followsKey = snapshot.getKey();
usersFollows.add(followsKey);
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

现在我必须进行查询以检索在此“usersFollows”ArrayList 中找到的具有“userId”值的每个帖子,但我真的不知道如何实现我的目标。例如,我不能写 Query query = postsReference.orderByChild("userId").equalTo(usersFollows); 因为你不能在 equalTo() 语句中传递一个数组。

我是在正确的道路上还是有更方便的方法?

最佳答案

不幸的是,firebase 不允许多个 equalTo()。我建议您以这种方式检查您的数据库:Posts -> Date -> UserId -> PostId 并添加一个 .read 规则来检查 Follow 列表中的 UserId 是否为真:

Posts: {
"$date" :{
"$userId": {
".read": "root.child(follows).child(auth.uid).child($userId).val() === true"
}
}
}

通过这种方式,您可以按日期获取帖子并自动缩小可见范围。

编辑:

您可以添加此规则来维护您的实际架构:

 Posts: {
"$post":{
".read": "root.child(follows).child(auth.uid).child(data.child(userId)).val() === true"
}
}

关于android - 从 Firebase 查询具有在 ArrayList 中找到的子值的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com