Colton Idle
10/31/2023, 1:52 PMfun loadData()
which hits the network then comes back and updates compose snapshot state. Works fine. Cool. Typically loadData is called from a LaunchedEffect.
BUT. Now I'm using a 3rd party sdk where it takes a lambda for initialization completion and so I want to move away from the LaunchedEffect and over to the sdk completion listener. So basically I call sdk.init( { viewModel.loadData() } )
. Cool. Everything still seems to work fine.
If I call loadData based on a compose launchedEffect and put a log
in loadData()
for Thread.current().name
I get main
. BUT after using the sdk.init()
and passing the lamda in, I get a different thread entirely. Should I be worried about updating my compose snapshot state from Thread-29
(or w/e) instead of thread main
?MR3Y
10/31/2023, 2:08 PMSnapshot.withMutableSnapshot { ... }
only if you're not on the main thread, https://kotlinlang.slack.com/archives/CJLTWPH7S/p1697173952963309?thread_ts=1697145056.766169&cid=CJLTWPH7Sstojan
10/31/2023, 2:25 PMsuspendCoroutine
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/suspend-coroutine.html
then you end up with
suspend fun initAndLoadData() {
sdk.awaitInit() // uses suspendCoroutine to convert from callback to suspend
loadData()
}
Zach Klippenstein (he/him) [MOD]
10/31/2023, 5:41 PMwithMutableSnapshot
to ensure all updates are seen by the main thread at the same timeColton Idle
11/01/2023, 6:44 PMUpdating state from another thread is fineCool. Is there a doc that points to that by any chance? Would like to share with my team vs just saying "Zach K says O.K." 😅
Zach Klippenstein (he/him) [MOD]
11/01/2023, 8:00 PMZach Klippenstein (he/him) [MOD]
11/01/2023, 8:01 PMColton Idle
11/01/2023, 8:48 PM