【发布时间】:2021-01-18 11:55:18
【问题描述】:
从 Cloud Firestore(红色)读取数据,该数据是静态数据并本地存储在房间数据库中
private val _fetchedJourneys = MutableLiveData<List<Journey>>()
override val fetchedJourneys: LiveData<List<Journey>>
get() = _fetchedJourneys
override suspend fun getJourneys() {
FirebaseFirestore.getInstance()
.collection(COLLECTION_JOURNEY)
.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val fetchedJourneys = arrayListOf<Journey>()
for (queryDocumentSnapshot in task.result!!) {
val journey = queryDocumentSnapshot.toObject(Journey::class.java)
journey.id = queryDocumentSnapshot.id
// At this point I have the journey ID, which in return I
// need to query the progress collection to look for the timestamp and add
// its value it to journey.lastOpened
fetchedJourneys.add(journey)
}
_fetchedJourneys.postValue(fetchedJourneys)
Timber.d("Fetched ${fetchedJourneys.size} journeys remotely")
} else {
Timber.e(task.exception!!.message, "Failed to fetch remote journeys")
}
}
}
对象:
@Entity(tableName = "journeys")
@IgnoreExtraProperties
@Parcelize
data class Journey(
@PrimaryKey(autoGenerate = false)
@SerializedName("journey_id")
var id: String = "",
val title: String = "",
val description: String = "",
val image: String = "",
@ColumnInfo(name = "last_opened")
var lastOpened: String = ""
) : Parcelable
还有一些“进度”数据是动态的,并且因用户而异。 progress 集合中的字段是一个时间戳,必须添加到Journey 对象的lastOpened 属性中,并且位于以 userId 作为 documentId 的文档中。
我是 firebase API 的初学者,我知道所有调用都是异步的,但我似乎想不出一种在调用时将属性“progress”(黄色)添加到对象(红色)的方法到对象。
我可以在应用程序中实现这一点,还是需要重新安排我保存数据的方式?
【问题讨论】:
-
为了更好地理解,请改写问题。进步不是财产
标签: kotlin mvvm google-cloud-firestore android-livedata kotlin-coroutines