Jeff Lockhart
02/12/2024, 6:37 PMCoroutineScope
doesn't use an immediate CoroutineDispatcher
by default, as is the case for Android's ViewModel
and Lifecycle
`CoroutineScope`s? Having an immediate dispatcher allows updating state synchronously, like for TextField
using a StateFlow
. See this thread for more examples and discussion. CC @Arkadii Ivanoveygraber
02/12/2024, 6:56 PMJeff Lockhart
02/12/2024, 7:27 PMDispatchers.Main.immediate
without the additional kotlinx-coroutines-swing
dependency, which isn't documented well. I'll create an issue in Google's tracker as well.Jeff Lockhart
02/12/2024, 7:43 PMDispatchers.Main.immediate
is used in some cases. But Dispatchers.Main
is used in others.Jeff Lockhart
02/12/2024, 7:51 PMCoroutineContext
in the remembered CoroutineScope
, is this context then used for all collectAsState
calls for that composable? Does that context get passed down to child composables and their `CoroutineScope`s as well? Or does the context need to be provided in all of these places explicitly?eygraber
02/12/2024, 9:02 PMscope
which is used internally. If I wanted the same behavior elsewhere I'd have to do the same thing. It hasn't come up yet, but if it did I'd probably extract it to a function and call it rememberImmediateCoroutineScope
or something like that. Alternatively I could hoist it out of the composition and make it a protected property of VicePortal
, but it would still have to be explicitly used.Zach Klippenstein (he/him) [MOD]
02/12/2024, 9:29 PMeygraber
02/12/2024, 10:11 PMZach Klippenstein (he/him) [MOD]
02/12/2024, 10:20 PMJeff Lockhart
02/12/2024, 11:19 PMStateFlow
or Channel
, but you actually need the emissions and collections to be synchronous. It's equally hard to reason with the current subtle behavior of TextField
using StateFlow
or a `Channel` on a non-immediate dispatcher. So I wouldn't say immediate dispatchers are always bad from this perspective. If it's the right tool, it makes sense.
Of course if there are API changes that address those needs, they could be used instead, like BTF2. But there are other use cases that don't have a solution yet that doesn't require an immediate dispatcher.Jeff Lockhart
02/12/2024, 11:24 PMCoroutineScope
? I'm not finding a ton of info about immediate dispatchers being inherently bad from my searches, just these few (1, 2, 3). The last one mostly describes misunderstanding the behavior.
The second one is the original PR, but the disclaimer comment Roman added has since been removed from the docs, which now describe how immediate is safe from stack overflows instead. Also compare isDispatchNeeded
docs then and now.shikasd
02/13/2024, 5:48 AMArkadii Ivanov
02/13/2024, 8:51 AMArkadii Ivanov
02/13/2024, 8:52 AM