Colton Idle
03/14/2023, 10:40 PMval myButtonLaunch = {
val myText = viewModel.getAsyncData()
MyClipboardService.copyIntoClipboard(myText)
}
The above doesn't run because the button click lambda isn't a coroutine scope.
What are my options here? Pass activity into getAsyncData()
and inside of getAsyncData I copyIntroClipboard?Kirill Grouchnikov
03/14/2023, 10:43 PMColton Idle
03/14/2023, 10:48 PMColton Idle
03/14/2023, 10:49 PMval myButtonLaunch = {
viewModel.getAsyncDataAndCopy { text -> MyClipboardService.copyIntoClipboard(text)
}
}
Kirill Grouchnikov
03/14/2023, 10:50 PMIan Lake
03/14/2023, 10:55 PMrememberCoroutineScope
? https://developer.android.com/jetpack/compose/side-effects#remembercoroutinescopeColton Idle
03/14/2023, 10:55 PMColton Idle
03/14/2023, 10:57 PMColton Idle
03/14/2023, 10:57 PMColton Idle
03/14/2023, 10:57 PMColton Idle
03/14/2023, 10:58 PMIan Lake
03/14/2023, 10:58 PMColton Idle
03/14/2023, 10:58 PMIan Lake
03/14/2023, 10:59 PMColton Idle
03/14/2023, 10:59 PMIan Lake
03/14/2023, 11:00 PMIan Lake
03/14/2023, 11:01 PMrememberCoroutineScope
is perfect for you since it does that cancellation for you.
If no, then you already need to have a pipeline to have your ViewModel call update some state that you can read in the new instance of your UIColton Idle
03/14/2023, 11:02 PMgetAsyncData()
and inside of getAsyncData I copyIntroClipboard?"
That would also be a mem leak?Colton Idle
03/14/2023, 11:04 PMColton Idle
03/14/2023, 11:04 PMFrancesc
03/15/2023, 2:06 AMsuspend
methods from your viewmodel to call from the UI. The viewmodel should expose a regular method that returns quickly and all it does is push an event to a queue, to be processed asynchornouslyIan Lake
03/15/2023, 2:08 AMFrancesc
03/15/2023, 2:08 AMrule of thumb
Colton Idle
03/17/2023, 5:08 PM