ursus
02/03/2021, 3:24 AMoverride fun saveSubscribers(subscribers: List<Subscriber>) {
val remotes = subscribers.associateByTo(mutableMapOf()) { it.id }
queries.transaction {
val locals = allSubscribers()
for (local in locals) {
val id = local.id
val remote = remotes[id]
if (remote != null) {
// Match in remotes
queries.updateAsLoadedSubscriber(remote)
remotes.remove(id)
} else {
// No match in remotes, delete it
queries.deleteSubscriber(id)
}
}
// Not match in locals, insert it
remotes.values.forEach {
queries.insertAsLoadedSubscriber(it)
}
}
}
its syncing logic, calculates what to insert, delete and update, nothing special