如何解决无法使用Firestore中的数据重新分配变量
我正在尝试使用从Firestore提取的数据重新分配var shopModel
。我正在从Firestore提取文档,并将所述数据投射到自定义对象-可以,但是var shopModel
从未重新分配。我显然缺少了一些东西,并感谢有人能帮助我。
ShopStore.kt
override fun getSingle(documentPath: String): ShopModel {
var shopModel = ShopModel()
firestore.firestoreSettings = FirebaseFirestoreSettings.Builder().build()
val document = firestore.collection("shops").document(documentPath)
document.get().addOnSuccessListener {
shopModel = it.toObject(ShopModel::class.java)!!
return@addOnSuccessListener
}
.addOnFailureListener{ exception ->
info("get failed with ",exception)
return@addOnFailureListener
}
return shopModel
}
ShopModel.kt
@Parcelize
data class ShopModel (
var uuid: String?,var name: String?
) : Parcelable {constructor() : this("","")}
编辑:
我修改了代码以使用协程,但仍然遇到相同的问题-变量没有重新分配。
ShopStore.kt 和协程
override fun getSingle(documentPath: String): ShopModel {
var shopModel = ShopModel()
firestore.firestoreSettings = FirebaseFirestoreSettings.Builder().build()
val document = firestore.collection("shops").document(documentPath)
GlobalScope.launch(Dispatchers.IO){
shopModel = document.get().await().toObject(shopModel::class.java)!!
}
return shopModel
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。