I ran into a problem which I don't really understand. Maybe it is trivial, maybe I am just too tired and therefore I can't understand how it works at the moment.
I have a Firestore query which returns a list of object. In the onResult
I try to store this result in a local variable so I can change its values later. My problem is that when I update this local variable somehow the original result changes too. I don't really understand how it is possible.
getParts(
carId = carId,
onResult = { vehicleParts ->
//store callback result in local variable
var updatedVehicleParts: List<VehiclePart> = mutableListOf()
if (vehicleParts != null) {
updatedVehicleParts = vehicleParts
}
//update local variable -> (newValues is a parameter of the function)
newValues.map { newPart ->
updatedVehicleParts.find { it.name == newPart.name }?.let {
it.currentHealth = newPart.maxLifeSpan
it.replacementTime = newPart.replacementTime
}
}
//here not only the local variable is updated BUT also the result of the callback
},
onError = {
}
)
I feel so stupid not recognizing something here...