【发布时间】:2021-07-17 19:49:42
【问题描述】:
当集合中的文档存在与否时,我想返回 True 或 False。但它总是返回 false。
if (checkItemDeleted(post)) {
// true, item still there
} else {
// false, item deleted
}
boolean result = false;
private boolean checkItemDeleted(Post post) {
mPostsCollection
.document(post.itemID)
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()){
if (task.getResult().exists()) {
// document exists
result = true;
} else {
// document not exists
result = false;
}
}
});
return result;
}
我还在学习中,谢谢你的帮助。
【问题讨论】:
-
您不能等待异步加载的数据。需要数据的代码需要在完成监听器中,或者从那里调用,或者使用 Kotlin 的
await。
标签: android kotlin google-cloud-firestore