I have a compose Button onClick handler that saves important information. It's too slow to just run it on the main thread. GlobalScope.launch{} pops out a warning about that API being delicate. I don't want the coroutine to be suspended and cancelled if the view is dismissed, so LaunchedEffect seems to be the wrong approach.
Where is the appropriate place to situate the coroutine scope?
c
Colton Idle
10/21/2021, 9:23 PM
I think it would be the ViewModel as that would have it's own scope that you could invoke.
t
tad
10/22/2021, 2:00 AM
I would inject or remember an app-global scope, basically
Dispatchers.Default + SupervisorJob()
tad
10/22/2021, 2:01 AM
ViewModels shouldn't outlive the composition imo
c
Csaba Kozák
10/22/2021, 7:28 AM
They should. This happens when you put the screen onto the backstack, or a configuration change happens and the Activity is recreated.
m
Michael Langford
10/25/2021, 8:54 PM
So sure, Csaba has a point they can outlive the composition, but for this thing, it's writing stuff to shared preferences, so I'm not sure who'd own the view model then, because if that write occurs then the screen is popped off...how would I then know to dispose of the view model?