Are you using coroutines inside your Composables? Are you making sure that you use rememberCoroutineScope?
rememberCoroutineScope() is useful whenever you need to have your coroutine canceled as soon as the Composable leaves the Composition.
In this example, at first, I'm using regular CoroutineScope to run progress bar updates. As you can see the problem is that I'm trying to cancel the update, but I'm still getting the success message. This is because regular CoroutineScope is not aware of my Composable's lifecycle. It will run even if the operation that it started is no longer relevant.
In the 2nd part, I'm using rememberCoroutineScope(). rememberCoroutineScope ensures that the coroutine lifecycle is tied to the progress bar Composable. The progress bar leaves the composition when I press "cancel" - so does the coroutine running the updates. A very simple and easy solution indeed.
Make sure you're using rememberCoroutineScope() whenever you need to have your coroutine cancelled when some UI element leaves the Composition.
You can find the code here Have you used rememberCoroutineScope already? Share your experience below
Comments