tezvyn:

Which Compose side-effect for a one-time coroutine action?

Curated by the Tezvyn teamSource: developer.android.comintermediate
Which Compose side-effect for a one-time coroutine action?

Tests if you can choose the right Compose side-effect for a one-time coroutine. A great answer names `LaunchedEffect(Unit)`, explains it runs once and cancels on exit. A red flag is suggesting `rememberCoroutineScope` directly in the composable body.

WHAT THIS TESTS: This question assesses your deep understanding of the Jetpack Compose side-effect system and its interaction with the composable lifecycle. The interviewer is looking for more than just naming an API; they want to see if you understand why a specific handler is correct, particularly concerning coroutine lifecycles, cancellation, and preventing redundant work during recomposition. It separates candidates who know the 'what' from those who know the 'why.'

A GOOD ANSWER COVERS: A senior-level answer should clearly and concisely hit four points in order. First, state that LaunchedEffect is the correct tool for this job. Second, specify using a constant key like Unit or true (LaunchedEffect(Unit) { ... }) to ensure the effect runs exactly once when the composable enters the composition, not on subsequent recompositions. Third, explain that LaunchedEffect provides a CoroutineScope, so you can directly call suspend functions. Fourth, highlight the critical safety feature: the coroutine is automatically cancelled when the LaunchedEffect leaves the composition, preventing memory leaks and orphaned network requests.

COMMON WRONG ANSWERS: The most common and serious mistake is suggesting rememberCoroutineScope and then calling scope.launch directly within the main body of the composable function. This will launch a new coroutine on every single recomposition, potentially triggering thousands of network requests and crashing the app. Another incorrect answer is using SideEffect { ... }, which is meant for non-suspending code that needs to run after every successful recomposition. Using DisposableEffect is also suboptimal; its primary purpose is managing non-coroutine resources that require an explicit onDispose cleanup block, making LaunchedEffect the more idiomatic and safer choice for coroutine-based side effects.

LIKELY FOLLOW-UPS: Be prepared for follow-ups like: 'What if you need the data to refetch when a user ID changes?' (Answer: Change the key from LaunchedEffect(Unit) to LaunchedEffect(userId)). 'When would you use rememberCoroutineScope instead?' (Answer: For launching coroutines in response to user events, like a button click, outside of the composition lifecycle). 'What's the difference between LaunchedEffect and produceState?' (Answer: produceState is for converting non-Compose state, like a Flow, into Compose State, while LaunchedEffect is for running general side-effect logic).

ONE CONCRETE EXAMPLE: To fetch user profile data only once, you would write: LaunchedEffect(Unit) { val userProfile = viewModel.fetchUserProfile() }. The Unit key guarantees this fetchUserProfile() suspend function is called only when the composable is first added to the screen. If the composable is removed, any ongoing network request within that coroutine block is automatically cancelled by Compose.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Which Compose side-effect for a one-time coroutine action? · Tezvyn