In Jetpack compose, we have the option of using rememberCoroutineScope()
as well as using the LaunchedEffect
composable in order to use coroutines / run suspend functions (show snackbars etc).
The convention I've adopted so far is to remember a single coroutine scope at the top of my compose tree, and pass it down via function arguments to places where it is needed. This vaguely seems like a good practice, but on the other hand it's adding extra noise to my function signatures.
LaunchedEffect
over rememberCoroutineScope()
inside composable functions?rememberCoroutineScope()
in each function where a coroutine is actually launched?Question 1: LaunchedEffect should be used when you want that some action must be taken when your composable is first launched/relaunched (or when the key parameter has changed). For example, when you want to request some data from your ViewModel or run some sort of animation...
rememberCoroutineScope: obtain a composition-aware scope to launch a coroutine outside a composable. rememberUpdatedState: reference a value in an effect that shouldn't restart if the value changes. DisposableEffect: effects that require cleanup. SideEffect: publish Compose state to non-compose code.
Side effects are undesirable because they can potentially change the state of the app outside the scope of the composable (global state). This basically means that the composable function might not behave the exact same way when called multiple times.
Leaving my understanding here:
Question 1:
LaunchedEffect
should be used when you want that some action must be taken when your composable is first launched/relaunched (or when the key parameter has changed). For example, when you want to request some data from your ViewModel or run some sort of animation...rememberCoroutineScope
on the other hand, is specific to store the Coroutine scope allowing the code to launch some suspend
function...
imho, the only relation between them is that you can also use a LaunchedEffect
to launch a coroutine...
Question 2: As you can see in the docs, rememberCoroutineScope
will keep the reference of the coroutine's scope in a specific point of the composition. Therefore, if a given composable is removed from the recomposition, that coroutine will be cancelled automatically. For instance, you have the following composable calls A -> B -> C
. If you remember the coroutine scope in C
and it is removed from the composition, the coroutine is automatically cancelled. But if you remember from A
, pass the scope through B
and C
, use this scope in C
, and then C
is removed, the coroutine will continue running (because it was remembered in A
)...
Use rememberCoroutineScope() when you are using coroutines and need to cancel and relaunch the coroutine after an event
Use LaunchedEffect() when you are using coroutines and need to cancel and relaunch the coroutine every time your parameter changes and it isn’t stored in a mutable state.
LaunchedEffect: run suspend functions in the scope of a composable
To call suspend functions safely from inside a composable, use the LaunchedEffect composable. When LaunchedEffect enters the Composition, it launches a coroutine with the block of code passed as a parameter. The coroutine will be cancelled if LaunchedEffect leaves the composition.
rememberCoroutineScope: obtain a composition-aware scope to launch a coroutine outside a composable
As LaunchedEffect is a composable function, it can only be used inside other composable functions. In order to launch a coroutine outside of a composable, but scoped so that it will be automatically canceled once it leaves the composition, use rememberCoroutineScope
More from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With