'Firestore Delete elements from a nested map kotlin
I have a collection, I want to delete 2 documents from this collection Paradas collection and delete it from the nested map too Nested map like in SQL, when you delete it from a table to delete it from elsewhere too.
I tried
fun deleteNestedParadas(lista: ArrayList<Parada> ) {
try {
val refrutas=Firebase.firestore.collection("rutas")
refrutas.get().addOnSuccessListener { taskruta ->
for (ruta in taskruta.documents) {
val rutaa = ruta.toObject(Ruta::class.java)
if (rutaa != null) {
for (parada in lista) {
if (rutaa.paradasruta!!.contains(parada.id)) {
refrutas.document(rutaa.id)
.update("paradasruta",{parada.id to delete()})
.addOnSuccessListener {
Log.e("DeleteNestedParada",
"Se han borrado las paradas correctamente")
}
.addOnFailureListener {
Log.e("DeleteNestedParada", it.message.toString())
}
}
}
}
}
}
} catch (e: Exception){
Log.e("Nested Collection", e.message.toString())
}
}
Edit: The error that appears is this: After i execute the code, the whole map changes to this: "arity:0" and appears this android error
it doesn't make sense
Solution 1:[1]
I found the answer. Thanks to RogelioMonter and Alex Mamo for your help. Saw a few posts before like how to do it but it didn't work, I wasn't simply passing the mapOf to update by doing that now it works.
fun deleteNestedParadas(lista: ArrayList<Parada> ) {
try {
val refrutas=Firebase.firestore.collection("rutas")
refrutas.get().addOnSuccessListener { taskruta ->
for (ruta in taskruta.documents) {
val rutaa = ruta.toObject(Ruta::class.java)
if (rutaa != null) {
for (parada in lista) {
if (rutaa.paradasruta!!.contains(parada.id)) {
refrutas.document(rutaa.id)
.update("paradasruta",mapOf{parada.id to FieldValue.delete()})
.addOnSuccessListener {
Log.e("DeleteNestedParada",
"Se han borrado las paradas correctamente")
}
.addOnFailureListener {
Log.e("DeleteNestedParada", it.message.toString())
}
}
}
}
}
}
} catch (e: Exception){
Log.e("Nested Collection", e.message.toString())
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kytelex |