在Firestore中,我有一个用户集合,并且在每个用户文档中存储了一个名为“收藏夹”的集合,其中包含标记为“收藏夹”(存储)的文档的ID。
例如:
in users/2pfV9FbtwPYFmQHz3KU2BKmhMr82/favorites I have multiple documents such as 7F9COWGW3Ww8FWiH8VTA and 8b8WogHzpqCkw0ZxMjOw
我想查询一个查询,该查询从一个名为stores的集合中返回所有具有相同docID的文档,其中包含这两个ID以及更多(不在收藏夹列表中)
A similar query will be
SELECT * FROM stores WHERE docID EXISTS IN favorites
我可以采取另一种方法来获取两个集合并手动对其进行过滤,但是我使用的是Firebase RecyclerView适配器,该适配器显示的所有数据均基于查询,这将使事情更加高效。
如何取得这样的结果?让我知道是否需要进一步的解释
我上述问题的可能答案是从“收藏夹”集合中获取所有文档ID并将它们放入列表中,然后使用whereIn函数将商店收藏集ID与“收藏夹”列表ID结合在一起
主要问题是使用Firebase RecyclerView时完全失去实时更新功能
favoriteList = new ArrayList<>();
favCollection.get() //get user favorites
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
favoriteList.add(0, document.getId());
Log.d("Favorites", document.getId() + " => " + document.getData());
}
query = storesCollection.whereIn(FieldPath.documentId(), favoriteList).orderBy("storeName");//FieldPath.documentId gives documents IDs in stores collection which will be filtered with the provided list
attachRecyclerViewAdapter();//Initiate adapter after favorites list completion
} else {
Log.d("Favorites", "Error getting documents: ", task.getException());
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句