从Firebase Firestore Kotlin异步检索数据

时间:2020-02-29 10:22:38

标签: firebase kotlin google-cloud-firestore kotlin-coroutines

我正在尝试将Firebase中的数据存储到一个变量中,以备后用,但它始终返回空结果。我了解这是因为从数据库中检索数据需要花费时间,我应该使用async /协程来执行此操作,但是我一辈子都找不到解决方法。

private suspend fun retrieveDataFromFireBase() {
    // Create a reference to the cities collection
    val usersCollection = db.collection("users")
    // Create a query against the collection.
    val query = usersCollection.orderBy("points", Query.Direction.DESCENDING).limit(10)
        .get()
        .addOnSuccessListener { documents ->
            for (document in documents) {
                userNameStorage.add(document.data?.getValue("fullname").toString())
                pointsStorage.add(document.data?.getValue("points").toString())
                if (document.contains("imageUrl")) {
                    imageURL.add(document.data?.getValue("imageUrl").toString())
                } else {
                    imageURL.add("null")
                }

            }
        }
        .addOnFailureListener { exception ->
            Log.w("error5566", "Error getting documents: ", exception)
        }
    }
}

有人可以帮帮我吗

0 个答案:

没有答案
相关问题