尝试检索列表时出现Cloud Firestore错误

时间:2018-07-11 00:01:26

标签: android firebase kotlin google-cloud-firestore

我开始在个人项目中使用Cloud Firestore,遇到了一个尝试从文档检索列表的问题,而我仍然设法从后端接收了一些数据,这在检查数据库时不够精确。

Collection
 DocumentN
     -otherStuff
     -usermatches:                <---- This is an array  
          (0): 
             -Score:0
             -Id:123 
          (1): 
             -Score:1
             -Id:456

架构

Firestore

我一直在尝试很多,下面的课程是我暂时解决错误 HashMap cannot be cast to parameter..,但是如果您有任何反馈,我不知道如何真正实现我所需要的或有任何建议请告诉我

Backend.kt

docRef = db.collection("userdata").document(user.user_id)
                docRef.get()
                        .addOnCompleteListener { task ->
                            if (task.isSuccessful) {

                                val document: DocumentSnapshot = task.result

                                val cedula = document.data!!["cedula"] as Long
                                val points = document.data!!["points"] as Long
 //                                    val usermatches = document.data!!["usermatches"] as HashMap<String, Any>
                                val alias = document.data!!["username"] as String

//                                     val usermatches = document.toObject(Usermatches::class.java)

                                val map: MutableMap<String, Any>? = document.data
                                for (entry in map!!.entries) {
                                    if (entry.key == "usermatches") {

                                        val list = entry.value as ArrayList<Any>

                                        for (each in list){

                                            for (entry2 in  each as HashMap<String, Any>){

                                                var home_score: Long = 0
                                                var winner: Long = 0
                                                var userchanged: Boolean = true
                                                var id: String = ""
                                                var away_score: Long = 0

                                                val usermatch = Usermatches(home_score, away_score, id, userchanged, winner)

                                                when {
                                                    entry2.key == "away_score" -> {
                                                        away_score = entry2.value as Long

                                                        usermatch.away_score = away_score
//                                                            println(away_score)
                                                    }
                                                    entry2.key == "home_score" -> {

                                                        home_score = entry2.value as Long
                                                        usermatch.home_score = home_score
//                                                            println(home_score)
                                                    }
                                                    entry2.key == "id" -> {
                                                        id = entry2.value as String
                                                        usermatch.id = id
//                                                            println(id)
                                                    }
                                                    entry2.key == "userchanged" -> {
                                                        userchanged = entry2.value as Boolean
                                                        usermatch.userchanged = userchanged
//                                                            println(userchanged)
                                                    }
                                                    entry2.key == "winner" -> {
                                                        winner = entry2.value as Long
                                                        usermatch.winner = winner
//                                                            println(winner)
                                                    }

                                                }
                                                mDisposable.add(usermatchesViewModel.insert(Usermatches(home_score, away_score, id, userchanged, winner))
                                                    .subscribeOn(Schedulers.io())
                                                    .subscribe{ println("Checking")})

                                            }
                                        }
                                    }
                                }

                            } else {
                                Log.d(TAG, "Error getting Usermatches ", task.exception)
                            }

                        }
            })

用户匹配数据类

@Entity
data class Usermatches(var home_score: Long,
                   var away_score: Long,
                   @ColumnInfo(name = "usermatches_id") var id: String,
                   var userchanged: Boolean,
                   var winner: Long) {
    @PrimaryKey(autoGenerate = true)
    var num_id: Long = 0
 }

2 个答案:

答案 0 :(得分:1)

  

-usermatches:<----这是一个数组

不是!的确,如果一个对象以array的形式存储在数据库中,则entry.value返回一个ArrayList而不是一个数组,但是在您的情况下,usermatches是一个map,术语包含01等其他地图。这就是为什么您也会收到该错误的原因。因此,为了解决此问题,您需要对usermatches映射进行两次迭代,一次获取映射,第二次获取这些映射中的值。

答案 1 :(得分:0)

正如@Alex Mamo所指出的,它确实是一个HashMap

我在上面的代码片段中所做的与我实际解决问题的方式很接近。

我正在第三个for循环中初始化我的对象,并使其保存来自Firestore数据库的不精确信息。

解决方案:

docRef = db.collection("userdata").document(user.user_id)
                docRef.get()
                        .addOnCompleteListener { task ->
                            if (task.isSuccessful) {

                                val document: DocumentSnapshot = task.result

                                val cedula = document.data!!["cedula"] as Long

                                val points = document.data!!["points"] as Long

                                val alias = document.data!!["username"] as String

                                val map: MutableMap<String, Any>? = document.data
                                for (entry in map!!.entries) {
                                    if (entry.key == "usermatches") {

                                        val list = entry.value as ArrayList<Any>

                                        var home_score: Long = 0
                                        var winner: Long = 0
                                        var userchanged: Boolean = true
                                        var id: String = ""
                                        var away_score: Long = 0

                                        for (each in list){

                                            val usermatch = Usermatches(home_score, away_score, id, userchanged, winner)

                                            for (entry2 in  each as HashMap<String, Any>){

                                                when {
                                                    entry2.key == "away_score" -> usermatch.away_score = entry2.value as Long
                                                    entry2.key == "home_score" -> usermatch.home_score = entry2.value as Long
                                                    entry2.key == "id" -> usermatch.id = entry2.value as String
                                                    entry2.key == "userchanged" -> usermatch.userchanged = entry2.value as Boolean
                                                    entry2.key == "winner" -> usermatch.winner = entry2.value as Long
                                                }

                                            }

                                            println("$usermatch")
                                            mDisposable.add(usermatchesViewModel.insert(usermatch)
                                                    .subscribeOn(Schedulers.io())
                                                    .subscribe{println("Checking")})
                                        }
                                    }
                                }

                            } else {
                                Log.d(TAG, "Error getting Usermatches ", task.exception)
                            }
                        }
            })