想使用 Firestore 減少我的應用程式中的讀取次數。所以我在我的集??合中創建了一個具有以下結構的檔案來保存 10 個“記錄”的值,我將通過一次讀取獲得 - 因為這個檔案的大小相當不錯,不用擔心單個檔案的 1MB 限制大小檔案。我正在使用云功能更新本檔案的內容。
name of collection: helperz
name of document: top10
field name in top10: tresholdCounter - this I need to check if a single map should be added to top10 or not
field name in top10: top10 . . array of maps
helperz/top10/tresholdCounter
helperz/top10/top10/0/author
helperz/top10/top10/0/name
helperz/top10/top10/0/url
helperz/top10/top10/1/author
helperz/top10/top10/1/name
helperz/top10/top10/1/url
helperz/top10/top10/2/author
helperz/top10/top10/2/name
helperz/top10/top10/2/url
helperz/top10/top10/3/author
helperz/top10/top10/3/name
helperz/top10/top10/3/url
helperz/top10/top10/4/author
helperz/top10/top10/4/name
helperz/top10/top10/4/url
..
helperz/top10/top10/10/author
helperz/top10/top10/10/name
helperz/top10/top10/10/url
我有一個資料類。. 像這樣:
data class MyClass(
var name: String? = null,
var url: String? = null,
var author: String? = null,
var counter: Int = 0,
var free: Boolean? = false,
var global: Boolean?=true,
var poses: ArrayList<MyPoze>? = null,
var docId: string? = null,
var category: ArrayList<String>? = null,
@get:PropertyName(CREATED)
@set:PropertyName(CREATED)
@ServerTimestamp var created: Timestamp? = null
)
還有一些其他領域,但對于這個問題,應該沒問題。
我有一個從 Firestore 檢索資料的代碼(在我的 viewModel 中):
private fun getHelperzTop10() = viewModelScope.launch {
Log.d("MP23", "getHelperzTop10")
val docRef = db.collection("helperz").document("top10")
docRef.addSnapshotListener { snapshot, e ->
if (e != null) {
Log.w("MP23", "Listen failed.", e)
return@addSnapshotListener
}
if (snapshot != null && snapshot.exists()) {
val docSize = firestoreDocument.getSize(snapshot);
Log.d("MP23","docSize in Bytes: $docSize, in KB: ${docSize * 0.001}, in MB: ${docSize * 0.000001}"
)
val top10 = snapshot.data
Log.d("MP23", top10!!::class.simpleName.toString() )
if ("top10" in keys) {
val top10arr = top10["top10"] as ArrayList<MyClass>
Log.d("MP23", "we have top10 in top10")
Log.d("MP23", top10arr!!::class.simpleName.toString())
Log.d("MP23", top10arr.toString())
//// ?? PROBLEM HERE ///
// here I need somehow to cast this array of maps as MyClass, but I am getting error:
// java.util.ArrayList cannot be cast to java.lang.MyClass
// or
// java.util.ArrayList cannot be cast to java.lang.Object[] - with some other versions
}
} else {
Log.d("MP23", "Current data: null")
}
}
}
如果有人可以建議我如何將結果轉換為 MyClass,那就太好了。在正常情況下(我有我查詢的檔案)作為單個檔案,我可以輕松地做到:
val singleRec = singleRec.toObject(MyClass::class.java)
uj5u.com熱心網友回復:
如果您想將top10陣列作為List<MyClass>,我能想到的最簡單的解決方案是創建另一個類:
data class Document(
var top10: MutableList<MyClass>? = null
)
并像這樣閱讀檔案的內容:
docRef.get().addOnCompleteListener {
if (it.isSuccessful) {
val snapshot = it.result
snapshot?.let {
val document = snapshot.toObject(Document::class.java)
document?.let {
val top10 = document.top10
top10?.let {
for (myClass in top10) {
val name = myClass.name
Log.d("TAG", "$name")
}
}
}
}
} else {
it.exception?.message?.let { message ->
Log.d("TAG", message) //Never ignore potential errors!
}
}
}
我還建議您閱讀以下資源以更好地理解:
- 如何將物件陣列從 Cloud Firestore 映射到物件串列?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/529193.html
標籤:Google Cloud Collective 安卓火力基地科特林谷歌云平台谷歌云火库
