【发布时间】:2021-01-01 10:56:23
【问题描述】:
从 Firestore 获取一次性数据时,我可以使用 kotlinx-coroutines-play-services 库删除回调:
使用回调
firestore.collection("Users")
.document(userId)
.get()
.addOnSuccessListener { snapshot ->
// Handle snapshot
}
.addOnFailureListener { exception ->
// Handle exception
}
使用await() 和协程
try {
val snapshot = firestore.collection("Users")
.document(userId)
.get().await()
// Handle snapshot
} catch(exception: Exception) {
// Handle error
}
我正在寻找一种类似的实时更新方法。 addSnapshotListener() 是否有 await() 等效项?
firestore.collection("Users")
.document(userId)
.addSnapshotListener { snapshot, exception ->
// Listen to snapshot in real time
}
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore kotlin-coroutines