iamthevoid
08/09/2022, 6:46 AMfun replaceAlerts(newAlerts: List<ExternalAlert>) {
val data = stateFlow.value
val existsAlerts = data.alerts.toMutableList()
newAlerts.forEach { newAlert ->
for (i in existsAlerts.indices) {
if (newAlert.id == existsAlerts[i].id) {
existsAlerts[i] = newAlert
return@forEach
}
}
existsAlerts.add(newAlert)
}
stateFlow.value = data.copy(alerts = existsAlerts)
}
Joffrey
08/09/2022, 7:25 AMiamthevoid
08/09/2022, 7:28 AMJoffrey
08/09/2022, 7:31 AMMap
data structure instead of a list for the current items. If you do this, you only need to iterate the list of incoming items to update, and you can find the corresponding item by id in the map in constant timeiamthevoid
08/09/2022, 7:42 AM