如何等待Firestore完成,然后返回布尔结果



当集合中的文档是否存在时,我希望返回True或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;
}

我还在学习,谢谢你的帮助。

在get调用完成之前返回结果。别忘了这是异步的。

尝试使用async和wait,而不是完整的侦听器。

相关内容

最新更新