myanmarking
01/11/2022, 10:28 AMDominaezzz
01/11/2022, 12:13 PMZach Klippenstein (he/him) [MOD]
01/11/2022, 3:45 PMSnapshot.withMutableSnapshot {}
.myanmarking
01/11/2022, 4:39 PMZach Klippenstein (he/him) [MOD]
01/11/2022, 4:45 PMColton Idle
01/11/2022, 5:29 PMChange notifications are not actually sent when a snapshot state object is changed, they're only sent when a snapshot is committedoh hm. TIL
Filip Wiesner
01/11/2022, 6:59 PMZach Klippenstein (he/him) [MOD]
01/11/2022, 7:15 PMmyanmarking
01/12/2022, 10:08 AMval list = mutableStateListOf<Unit>()
Snapshot.withMutableSnapshot {
list.sortBy { } ..
}
?Colton Idle
01/12/2022, 6:20 PMmyanmarking
01/12/2022, 6:21 PMColton Idle
01/12/2022, 6:25 PMVM {
init {
viewModelScope.launch {
firestore.observeTodoItems {
val sorted = it.sort()
state.todos.clear()
state.todos.addAll(sorted)
}
}
}
}
Filip Wiesner
01/25/2022, 8:55 PMSnapshot.withMutableSnapshot { ... }
.Zach Klippenstein (he/him) [MOD]
01/26/2022, 2:25 AMval counter by mutableStateOf(0)
val iterations = 10_000
val parallelism = 5
repeat(parallelism) {
launch(Dispatchers.Default) {
doWork()
}
}
suspend fun doWork() {
repeat(iterations) {
counter = counter + 1
}
}
Filip Wiesner
01/26/2022, 6:57 AM.... If you're doing it off the main thread, you can wrap in a.Snapshot.withMutableSnapshot {}
when talking about sorting a list in place. I thought the reason is that the global snapshot could be advanced during this operation resulting in frame that has only part of the list sorted. Is that not the reason? If it is, how can I show it in an example? Because while your example show the benefits of Snapshots, it's usually not this extreme in real world scenario. I've been reading about and playing with snapshots (in Compose UI) for a while but I feel like something is still not clicking for me. You know the feeling, like I'm missing something simple and obvious but I am just overthinking it.
Zach Klippenstein (he/him) [MOD]
02/01/2022, 3:17 AMArsen
02/07/2022, 1:45 PMval counter = mutableStateOf(0, object : SnapshotMutationPolicy<Int> {
override fun equivalent(a: Int, b: Int) = false // comparison make no sense
override fun merge(previous: Int, current: Int, applied: Int): Int {
return current + (applied - previous)
}
})
fun equivalent(a: Int, b: Int): Boolean = a == b
which looks weird (for multithreaded environment). Imagine case:
Snapshot1 reads 0
Snapshot2 reads 0
Snapshot1 increment
Snapshot2 increment
Snapshot1 apply ->
equivalent(0, 1) ->
merge(0, 0, 1)
Snapshot2 apply ->
equivalent(1, 1) -> skip; // lost 1
But even my variant with equivalent() = false
still produces weird results (e.g. 98 instead of 100). I guess it's due to`optimisticMerges`