Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何等待Firestore完成,然后返回布尔结果?_Android_Kotlin_Google Cloud Firestore - Fatal编程技术网

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

Android 如何等待Firestore完成,然后返回布尔结果?,android,kotlin,google-cloud-firestore,Android,Kotlin,Google Cloud Firestore,当集合中的文档存在或不存在时,我希望返回True或False。但它总是返回false if (checkItemDeleted(post)) { // true, item still there } else { // false, item deleted } 我仍在学习中,谢谢您的帮助。您将在get呼叫完成之前返回结果。不要忘记这是异步的 尝试使用async and wait代替完整的侦听器。您不能等待异步加载的数据。需要数据的代码需要在完成侦听器中,或者从那里调用,或者使用K

当集合中的文档存在或不存在时,我希望返回True或False。但它总是返回false

if (checkItemDeleted(post)) {
   // true, item still there
} else {
  // false, item deleted
}

我仍在学习中,谢谢您的帮助。

您将在get呼叫完成之前返回结果。不要忘记这是异步的


尝试使用async and wait代替完整的侦听器。

您不能等待异步加载的数据。需要数据的代码需要在完成侦听器中,或者从那里调用,或者使用Kotlin的wait
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;
    }