I have this composable that represents an "edit data" screen:
@Composable
fun EditNodeScreen(
vm: EditNodeViewModel,
canceled: () -> Unit,
accepted: (id: UUID) -> Unit
) {
// ...
Button(onClick = {
val id = vm.save()
accepted(id)
}) {
Text(text = "Save")
}
}
Except, EditNodeViewModel.save()
is actually a suspend function, so I can't just call it like that.
What I can find says that I should create a coroutine scope with rememberCoroutineScope()
, then use that to launch a coroutine:
onClick = {
coroutineScope.launch {
val id = vm.save()
accepted(id) // side question: do I have to switch back to Main context?
}
}
But the documentation also says that this coroutine will be canceled if the composition is detached. I do not want to cancel the save process once it is commenced!
Is this still the right thing to do, or is there a better way? Should I use GlobalScope.launch
perhaps?
If you have to handle an operation that should be completed even if the user navigates away from the screen, use WorkManager
.
From the Docs ,
WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts.
Use Expedited work to start the task immediately.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.