简体   繁体   English

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

[英]How to check if Cloud Firestore collection exists? ( querysnapshot)

I'm having trouble with checking if my collections exists in Firestore database. 我在检查Firestore数据库中是否存在我的收藏集时遇到麻烦。 When I was working with Firebase Realtime database i could have used: 当我使用Firebase Realtime数据库时,我可以使用:

if(databaseSnapshot.exists) 

Now with Firestore I wanna do the same. 现在,在Firestore中,我想做同样的事情。 I have already tried 我已经尝试过

  if (documentSnapshots.size() < 0) 

but it doesn't work. 但这不起作用。 Here is the current code: 这是当前代码:

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();
            }
        }
    });
}

the function: setNothingToShow(); 函数:setNothingToShow(); Is actually what I wanna execute if my collection is empty / doesn't exists. 实际上是我要执行的操作,如果我的收藏集为空/不存在。 Please advise! 请指教! Thanks, D. 感谢:D。

exists() applies to DocumentSnapshot while you're dealing with QuerySnapshot 在处理QuerySnapshotQuerySnapshot exists()适用于DocumentSnapshot

Call task.result for getting QuerySnapshot out of Task<QuerySnapshot> . 调用task.result以将QuerySnapshotTask<QuerySnapshot>

From that, call result.getDocuments() and iterate through each of the DocumentSnapshot calling exists() on them. 从这一点,调用result.getDocuments()并通过每个迭代的DocumentSnapshot呼叫exists()他们。

Use DocumentSnapshot.size() > 0 to check if the collection exists or not. 使用DocumentSnapshot.size() > 0来检查集合是否存在。

Here is an example from my code: 这是我的代码中的一个示例:

  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());
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? Cloud Firestore:查询以检查用户名是否存在 - Cloud Firestore: Query to check if a username exists 如何检查该字段是否存在于 firestore 中? - How to check if the field exists in firestore? "如何从 android firestore 中的 QuerySnapshot 获取密钥" - How to get key from QuerySnapshot in android firestore 如何通过云触发 Firestore 收集 function - how to trigger a firestore collection via cloud function 在将文档接受到集合Cloud Firestore中之前尝试添加支票 - Trying to add a check before accepting a document into a collection Cloud Firestore 有没有办法使用安全规则检查 Cloud Firestore 集合的大小? - Is there a way to check the size of a Cloud Firestore collection using Security Rules? 如何检查带有动态名称的参数/集合是否存在? - How to check if argument/collection with dynamic name exists? 如何检查GrantedAuthority集合中是否存在授权? - How to check if authority exists in a collection of GrantedAuthority? 如果使用集合的 Firestore 中不存在数据 - If data not exists in the Firestore using Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM