Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rememberCoroutineScope() vs LaunchedEffect

Context

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.

Questions

  1. Are there any reasons for preferring the use of LaunchedEffect over rememberCoroutineScope() inside composable functions?
  2. Is it worth the effort to only create / remember a coroutine scope once per compose tree, or should I just call rememberCoroutineScope() in each function where a coroutine is actually launched?
like image 634
machfour Avatar asked Mar 04 '21 11:03

machfour


People also ask

When should I use LaunchedEffect?

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...

What is rememberCoroutineScope?

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.

Why is it important for composable functions to be free of side effects?

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.


3 Answers

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)...

like image 154
nglauber Avatar answered Oct 17 '22 03:10

nglauber


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.

like image 3
Hamdy Abd El Fattah Avatar answered Oct 17 '22 03:10

Hamdy Abd El Fattah


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

like image 2
T D Nguyen Avatar answered Oct 17 '22 04:10

T D Nguyen