【发布时间】:2020-04-09 05:35:20
【问题描述】:
再次 :) 我有一个关于 Cloud Firestore 和 Kotlin 的问题。
我需要使用如下代码从 firestore 获取数据:
{
val comments = mutableListOf<Comment>()
val firestore = FirebaseFirestore.getInstance()
firestore.collection(collection).document(documentID).collection("comments")
.addSnapshotListener { querySnapshot, firebaseFirestoreException ->
comments = querySnapshot?.toObjects(Comment::class.java)!!
// do something with 'comments'. Works: comments is populated
}
// do something with variable 'comments'. Doesn't work: comments is now empty
}
变量 'cmets' 填充在侦听器大括号内,但当侦听器结束时,该值又回到 0。
我在网上进行了研究,发现 JAVA 中的示例可以完美地以这种方式运行,例如: https://youtu.be/691K6NPp2Y8?t=246
我的目的是仅从 Cloud Firestore 中获取一次数据,并将该值存储在全局变量 cmets 中。
如果您对此有解决方案,请告诉我。
谢谢。
【问题讨论】:
标签: android kotlin google-cloud-firestore