反序列化包含数组的Firestore文档的推荐方法是什么?
我试图获取包含数组的Firestore文档,但是,我无法使DocumentSnapshot.toObject
方法在我添加数组后立即正常工作。 我不想使用集合,因为数组可能最多只包含1-2个项目。
java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type com.google.android.gms.internal.zzegf to DocumentReference (found in field 'references.[0]')
以下是我的模特班
data class SomeModel( var references : ArrayList<DocumentReference> = ArrayList(0), var title : String? = null, var acronym : String? = null, var number : Long? = null ){ }
我的Firestore文档包含一个名为references
的数组,其中包含一个DocumentReference
。 如果我从模型类中删除引用字段,对象反序列化就好了。
如果我想检索单个文件内的项目列表,我可以做到:
包含字符串列表的数据类,列表可以是任何类型
注意: – 我提供了所有参数的默认值,反序列化是必要的类有空承包商。
data class SomeModel(val references : List<String> = emptyList() , val title : String = "" , val acronym : String = "" , val number : Long = 0L)
那么我可以指向这个单一的文件,并反序列化它包括在这个文件中的文件列表: –
val db = FirebaseFirestore.getInstance() val someModelRef = db.collection("someCollection").document("SomeModel") someModelRef.get().addOnSuccessListener { val someModel : SomeModel = it.toObject(SomeModel::class.java) someModel.references.forEach { Log.i("MainActivity","items $it") } }
firestore数据库中的数据: –
当我运行上面的代码在我的logcat的输出
- Android Studio跳过断点
- 在Kotlin中定义log TAG常量的最好方法是什么?
- ImageButton OnClickListener不工作
- Android Retrofit + Google Places API(文本搜索)
- 使用Kotlin进行数据绑定会导致Resources $ NotFoundException
- Kotlin中的Activity Activity静态启动器方法
- 图像存在,不为空,但我得到一个NullPointerException异常
- butterknife 7.x和Kotlin M14一起工作吗?
- 未解决的参考:LinearLayoutManager / RecyclerView