小编Whi*_*nja的帖子

如何检查Cloud Firestore集合是否存在?(查询快照)

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

if(databaseSnapshot.exists) 
Run Code Online (Sandbox Code Playgroud)

现在,我想通过 Firestore 做同样的事情。我已经尝试过了

  if (documentSnapshots.size() < 0) 
Run Code Online (Sandbox Code Playgroud)

但它不起作用。这是当前的代码:

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();
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

函数:setNothingToShow(); 如果我的集合为空/不存在,这实际上是我想要执行的。请指教!感谢:D。

java database android firebase google-cloud-firestore

3
推荐指数
1
解决办法
1万
查看次数