Reputation: 615
I started working with Cloud Firestore in a personal project and have been facing an issue trying to retrieve a List from a Document while I still managed to receive some data from the backend, it's not that precise while checking the DB.
Collection
DocumentN
-otherStuff
-usermatches: <---- This is an array
(0):
-Score:0
-Id:123
(1):
-Score:1
-Id:456
Schema
I've been trying alot and the class below is me solving momentarily the Error HashMap cannot be cast to parameter..
but I do not know how to really achieve what i need, if you have any feed back or have any recommendations pls tell me
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)
}
}
})
Usermatches Data Class
@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
}
Upvotes: 1
Views: 436
Reputation: 615
As @Alex Mamo pointed out, it was indeed a HashMap
What I was doing in the above code snipet was close to how I actually solved the problem.
I was initializing my Object inside the third for loop and was making it hold imprecise information from the Firestore DB.
Solution:
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)
}
}
})
Upvotes: 0
Reputation: 138824
-usermatches: <---- This is an array
Is not! It's true that if an object is stored in the database as an array
, entry.value
returns an ArrayList and not an array, but in your case, usermatches
is a map
which in terms contains other maps, 0
, 1
and so on. That's why you also get that error. So in order to solve this, you need to iterate over usermatches
map twice, once to get the maps and second to get the values whithin those maps.
Upvotes: 1