反序列化包含数组的Firestore文档的推荐方式是什么?

反序列化包含数组的Firestore文档的推荐方式是什么?

问题描述:

我试图获取包含数组的Firestore文档,但是,我无法让DocumentSnapshot.toObject方法在我添加数组后立即正常工作。我不想使用集合,因为数组可能最多只包含1-2个项目。反序列化包含数组的Firestore文档的推荐方式是什么?

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的数组。如果我从模型类中删除引用字段,对象反序列化就好了。

将我的firebase-firestore的gradle文件条目从11.4.2更新到11.6.0,修复了反序列化问题。

,如果我想检索单个文档中的产品清单我可以这样做: -

包含字符串列表中的数据类,这个名单可以是任何类型

注: - 我提供一个默认所有参数的值,反序列化它是必要的类有空的承包商。

data class SomeModel(val references : List<String> = emptyList() , val title : String = "" , val acronym : String = "" , val number : Long = 0L) 

然后我可以指向这个单个文档和反序列化它包括文件itmes的名单是这样的: -

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数据库中的数据: -

enter image description here

当我运行上面的代码时,我logcat中的输出

enter image description here

+0

正确,但不是字符串列表,而是其他文档的Firebase DocumentReference数组。我将尝试使用List 和emptyList – John