Whi*_*nja 3 java database android firebase google-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。
用于DocumentSnapshot.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());
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11443 次 |
| 最近记录: |