min
09/18/2025, 6:56 AMGlobalSnapshotManager
(in androidx.compose.ui.platform
) start a coroutine on AndroidUiDispatcher.Main
?min
09/18/2025, 6:59 AMandroidx.compose.ui.platform
implementation:
internal object GlobalSnapshotManager {
private val started = AtomicBoolean(false)
private val sent = AtomicBoolean(false)
fun ensureStarted() {
if (started.compareAndSet(false, true)) {
val channel = Channel<Unit>(1)
CoroutineScope(AndroidUiDispatcher.Main).launch {
channel.consumeEach {
sent.set(false)
Snapshot.sendApplyNotifications()
}
}
Snapshot.registerGlobalWriteObserver {
if (sent.compareAndSet(false, true)) {
channel.trySend(Unit)
}
}
}
}
}
mine:
private object MyGlobalSnapshotManager {
private val started = AtomicBoolean(false)
private val pending = AtomicBoolean(false)
fun start() {
val manager = this
if (!manager.started.compareAndSet(false, true)) {
return
}
Snapshot.registerGlobalWriteObserver {
if (manager.pending.compareAndSet(false, true)) {
manager.pending.set(false)
Snapshot.sendApplyNotifications()
}
}
}
}
What does the androidx.compose.ui.platform
implementation achieve by spawning a coroutine that mine fails to achieve? Their implementation seems to stop listening once the AndroidUiDispatcher.Main
executor stops advancing the coroutine state, but why is it necessary to arrange that? If the main executor is ending, doesn’t that mean the app is closing? Why the need to specify ‘I’ll stop listening when the main thread is finishing’ – I couldn’t possibly keep listening if I wanted toshikasd
09/19/2025, 4:54 PMmin
09/20/2025, 6:28 AMmin
09/22/2025, 5:19 AMshikasd
09/22/2025, 10:25 AM