Java 如何检查云Firestore集合是否存在?(querysnapshot)

Java 如何检查云Firestore集合是否存在?(querysnapshot),java,android,database,firebase,google-cloud-firestore,Java,Android,Database,Firebase,Google Cloud Firestore,我在检查Firestore数据库中是否存在我的收藏时遇到问题。 当我使用Firebase实时数据库时,我可以使用: if(databaseSnapshot.exists) if (documentSnapshots.size() < 0) 现在对于Firestore,我也要这么做。 我已经试过了 if (documentSnapshots.size() < 0) if(documentSnapshots.size()0检查集合是否存在 if (document

我在检查Firestore数据库中是否存在我的收藏时遇到问题。 当我使用Firebase实时数据库时,我可以使用:

if(databaseSnapshot.exists) 
  if (documentSnapshots.size() < 0) 
现在对于Firestore,我也要这么做。 我已经试过了

  if (documentSnapshots.size() < 0) 
if(documentSnapshots.size()<0)
但它不起作用。 以下是当前代码:

  if (documentSnapshots.size() < 0) 
public void pullShopItemsFromDatabase() {
    mShopItemsRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    ShopItem shopItem = document.toObject(ShopItem.class);
                    shopItems.add(new ShopItem(shopItem.getImageUrl(), shopItem.getTitle(), shopItem.getSummary(), shopItem.getPowerLinkID(), shopItem.getlinkToShopItem(),shopItem.getLinkToFastPurchase(), shopItem.getKey(), shopItem.getPrice(),shopItem.getVideoID()));
                }
                if (shopItems != null) {
                    Collections.sort(shopItems);
                    initShopItemsRecyclerView();
                }
            } else {
                Log.w(TAG, "Error getting documents.", task.getException());
                setNothingToShow();
            }
        }
    });
}
public void pullShopItemsFromDatabase(){
mShopItemsRef.get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot文档:task.getResult()){
ShopItem ShopItem=document.toObject(ShopItem.class);
添加(新ShopItem(ShopItem.getImageUrl(),ShopItem.getTitle(),ShopItem.getSummary(),ShopItem.getPowerLinkID(),ShopItem.getlinkToShopItem(),ShopItem.getKey(),ShopItem.getPrice(),ShopItem.getVideoID());
}
如果(shopItems!=null){
收集。分类(商店物品);
initShopItemsRecyclerView();
}
}否则{
Log.w(标记“获取文档时出错”,task.getException());
设置Nothingtoshow();
}
}
});
}
函数:setNothingToShow(); 如果我的集合为空/不存在,实际上就是我想要执行的。 请告知! 谢谢 D.

存在()
适用于处理
查询快照时的
文档快照

  if (documentSnapshots.size() < 0) 
调用task.result以从
任务中获取
QuerySnapshot

  if (documentSnapshots.size() < 0) 

在此基础上,调用
result.getDocuments()
并遍历每个
DocumentSnapshot
调用
exists()

使用
DocumentSnapshot.size()>0
检查集合是否存在

  if (documentSnapshots.size() < 0) 
下面是我的代码中的一个示例:

  if (documentSnapshots.size() < 0) 
  db.collection("rooms").whereEqualTo("pairId",finalpairs)
         .get()
         .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                if(task.getResult().size() > 0) {
                    for (DocumentSnapshot document : task.getResult()) {
                        Log.d(FTAG, "Room already exists, start the chat");

                    }
                } else {
                    Log.d(FTAG, "room doesn't exist create a new room");

                }
            } else {
                Log.d(FTAG, "Error getting documents: ", task.getException());
            }
        }
    });
db.collection(“房间”)。whereEqualTo(“pairId”,finalpairs)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
if(task.getResult().size()>0){
对于(DocumentSnapshot文档:task.getResult()){
Log.d(FTAG,“房间已经存在,开始聊天”);
}
}否则{
Log.d(FTAG,“房间不存在,创建新房间”);
}
}否则{
Log.d(FTAG,“获取文档时出错:”,task.getException());
}
}
});

如果(documentSnapshots.size()<0)
将永远不会为真。它应该是
if(documentSnapshots.size()==0)
if(documentSnapshots.size()>0)
(取决于您放置它的位置)。@FrankvanPuffelen OK!我不知道发生了什么,但现在它起作用了。我以前尝试过在任务完成并成功后使用
onSuccess
激发。由于不需要加载任何数据而完成的任务也成功了。所以,
onSuccess
也会在那里起作用。@FrankvanPuffelen再次感谢。我看了CloudFireStore的介绍视频,他在“新”关键字之前使用了“OnSuccess”上的“this”侦听器。所有这些都在onStart上实现自动刷新内容。当在片段上使用此方法时,我注意到它会破坏片段事务,我假设因为它与几个onStart方法冲突(导致每个片段都实现了它),有什么建议可以避免它吗?我对事务生命周期不太熟悉,因此无法提供帮助。谢谢!事实上,我发现了一种不同的方法,但它确实有助于未来的需要。@DaniyyelTouboul如果这个答案有帮助,你应该投票支持它。只有当它回答了你提出的问题时,你才应该接受它,但它似乎没有回答。@FrankvanPuffelen不幸的是,我还不能投票。我的用户仍然有限。@WhiteNinja你能分享你的方法吗?