benkuly
02/28/2023, 5:39 PMscope.launch(Dispatchers.Main.immediate) {
navigation.replaceCurrent(...)
}
We do that do prevent runtime exceptions due to wrong coroutine context by some caller of a public API... is there a reason, why decompose enforces the main dispatcher?Arkadii Ivanov
02/28/2023, 5:51 PMlaunch
just to navigate suggests that something is not good with data flows in your projects. Usually you navigate in response to UI events (those are already on the main thread). Or you observe some events - in this case you should explicitly specify the Main dispatcher (or observeOn(mainScheduler)
with Rx).
E.g.
mainScope.launch {
events.collect {
// Navigate here
}
}
benkuly
03/01/2023, 8:30 AMsuspend fun <C : Any> StackNavigator<C>.popSuspending(
onComplete: (isSuccess: Boolean) -> Unit = {}
) = withContext(Dispatchers.Main.immediate) {
pop(onComplete)
}
Could it make sense to add this to decompose? We don't want to force users of our API to have knowledge about decompose. They just should call for example fun okButtonPressend()
without to worry, if they call it from within a coroutine and switch context or not. The implementation on our side currently would be fun okButtonPressed() = scopeWithLifecycle.launch { popSuspsending() }
.Arkadii Ivanov
03/01/2023, 9:19 AMCould it make sense to add this to decompose?I'm afraid no. But you can add those as extensions specifically for your project.
We don't want to force users of our API to have knowledge about decompose.This is a good decision. I agree.
They just should call for exampleBut this is something I would think twice about. 1.without to worryfun okButtonPressend()
onBackButtonPressed
name means that the back button was pressed. Usually back buttons are pressed on the main thread. If the client calls this methods from another thread, either they are abusing the API or the method should be renamed (e.g. goBack
).
2. You can still force your users to use the correct thread without telling them you are using Decompose. The same story with Android Views, Swing and many other UI frameworks. You are required to update the UI on the UI thread. As a developer and a user of that API, it's your responsibility. Delegating thread switching to the UI framework is very error-prone and may lead to hard-to-debug race conditions in client's application.
So, if you really believe this is the good way for your API, then please add extensions with thread switching specifically for your project.