Share
Embed
Avoiding common coroutine mistakes in Compose (KotlinConf '23)
Compose and coroutines work great together, but there are certain patterns you need to avoid when combining them. In this session, we’ll look at some common pitfalls, detail why and how they cause problems, and see what patterns or APIs should be used instead.
More info and resources: https://zsmb.co/appearances/kotlinconf-2023-day2/
Márton Braun
April 14, 2023
Transcript
63 slides
-
Márton Braun @zsmb13 Developer Relations Engineer Google Avoiding common coroutine
mistakes in Compose KotlinConf’23 Amsterdam -
Avoiding common coroutine mistakes in Compose Data layer UI layer
UI elements ViewModel -
Avoiding common coroutine mistakes in Compose UI layer ViewModel Data
layer UI elements -
Column { Row { Icon() Text() } Image() Row {
Column { Text() Text() } } } Composition goo.gle/mad-skills-compose-phases -
Column { Row { Icon() Text() } Image() Row {
Column { Text() Text() } } } Composition goo.gle/mad-skills-compose-phases -
Composable functions can... • Be called very frequently • Execute
in any order • Run in parallel* -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } In the Composition Not in the Composition -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { val coroutineScope = rememberCoroutineScope() Column { coroutineScope.launch { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } In the Composition Not in the Composition -
@Composable fun HomeScreen( time: String, connected: Boolean, snackbarHostState: SnackbarHostState, )
{ Column { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } @Composable fun HomeScreen( time: String, connected: Boolean, snackbarHostState: SnackbarHostState, ) { Column { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } Creating coroutines in the composition val coroutineScope = rememberCoroutineScope() coroutineScope.launch { -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { Column { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } LaunchedEffect() { -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { Column { LaunchedEffect() { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
Creating coroutines in the composition @Composable fun HomeScreen( time: String,
connected: Boolean, snackbarHostState: SnackbarHostState, ) { Column { LaunchedEffect(connected, snackbarHostState) { val message = if (connected) "Connected and ready!" else "No connection :(" snackbarHostState.showSnackbar(message) } Text(text = "It's $time, a great time to order a pizza!") } } -
A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier =
Modifier) { Box(modifier) { val listState = rememberLazyListState() LazyColumn(state = listState) { /* Show pizza items... */ } FloatingActionButton( onClick = { listState.animateScrollToItem(0) } ) { Icon(Icons.Filled.ArrowUpward) } } } -
A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier =
Modifier) { Box(modifier) { val listState = rememberLazyListState() LazyColumn(state = listState) { /* Show pizza items... */ } FloatingActionButton( onClick = { listState.animateScrollToItem(0) } ) { Icon(Icons.Filled.ArrowUpward) } } } -
A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier =
Modifier) { Box(modifier) { val listState = rememberLazyListState() LazyColumn(state = listState) { /* Show pizza items... */ } FloatingActionButton( onClick = { listState.animateScrollToItem(0) } ) { Icon(Icons.Filled.ArrowUpward) } } } -
A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier =
Modifier) { Box(modifier) { val listState = rememberLazyListState() LazyColumn(state = listState) { /* Show pizza items... */ } FloatingActionButton( onClick = { listState.animateScrollToItem(0) } ) { Icon(Icons.Filled.ArrowUpward) } } } -
FloatingActionButton( onClick = { } ) { Icon(Icons.Filled.ArrowUpward) } }
} listState.animateScrollToItem(0) A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier = Modifier) { Box(modifier) { val listState = rememberLazyListState() LazyColumn(state = listState) { /* Show pizza items... */ } -
A positive example FloatingActionButton( onClick = { } ) {
Icon(Icons.Filled.ArrowUpward) } } } val scope = rememberCoroutineScope() listState.animateScrollToItem(0) LazyColumn(state = listState) { /* Show pizza items... */ } scope.launch { } @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier = Modifier) { Box(modifier) { val listState = rememberLazyListState() -
A positive example @Composable fun PizzaList(pizzas: List<Pizza>, modifier: Modifier =
Modifier) { Box(modifier) { val listState = rememberLazyListState() val scope = rememberCoroutineScope() LazyColumn(state = listState) { /* Show pizza items... */ } FloatingActionButton( onClick = { scope.launch { listState.animateScrollToItem(0) } } ) { Icon(Icons.Filled.ArrowUpward) } } } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { } 1 2 3 4 5 6 7 8 9 10 numbers -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } } 1 2 3 4 5 6 7 8 9 10 numbers phaseFlow 1 2 3 4 5 6 7 0 1 2 map -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } val phase by phaseFlow.collectAsState(initial = 0) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } val phase: Int by phaseFlow.collectAsState(initial = 0) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = numbers.map { it % 8 } val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } numbers.map { it % 8 } -
Creating Flows in the composition numbers.map { it % 8
} @Composable fun RotatedPizzaSlice(phase: Int) @Composable fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = remember(numbers) { } val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } numbers.map { it % 8 } -
Creating Flows in the composition @Composable fun RotatedPizzaSlice(phase: Int) @Composable
fun PizzaLoader(numbers: Flow<Int>) { val phaseFlow = remember(numbers) { } val phase by phaseFlow.collectAsState(initial = 0) RotatedPizzaSlice(phase) } numbers.map { it % 8 } -
Using StateFlow values class PizzaViewModel( repository: PizzaRepository ) : ViewModel()
{ val pizzas: StateFlow<List<Pizza>> = repository.getAllPizzas() .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000L), initialValue = PizzaRepository.DEFAULT_PIZZAS, ) } -
Using StateFlow values class PizzaViewModel( repository: PizzaRepository ) : ViewModel()
{ val pizzas: StateFlow<List<Pizza>> = repository.getAllPizzas() .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000L), initialValue = PizzaRepository.DEFAULT_PIZZAS, ) } -
Using StateFlow values @Composable fun PizzaList(pizzas: List<Pizza>) @Composable fun LivePizzaList(vm:
PizzaViewModel) { val prices = vm.pizzas.value PizzaList(prices) } -
Using StateFlow values @Composable fun PizzaList(pizzas: List<Pizza>) @Composable fun LivePizzaList(vm:
PizzaViewModel) { val prices = vm.pizzas.value PizzaList(prices) } -
Using StateFlow values @Composable fun PizzaList(pizzas: List<Pizza>) @Composable fun LivePizzaList(vm:
PizzaViewModel) { val prices = vm.pizzas.value PizzaList(prices) } -
Using StateFlow values @Composable fun PizzaList(pizzas: List<Pizza>) @Composable fun LivePizzaList(vm:
PizzaViewModel) { val prices by vm.pizzas.collectAsState() PizzaList(prices) } -
Using StateFlow values @Composable fun PizzaList(pizzas: List<Pizza>) @Composable fun LivePizzaList(vm:
PizzaViewModel) { val prices by vm.pizzas.collectAsStateWithLifecycle() PizzaList(prices) } goo.gle/consume-flows-safely -
Summary • Watch out for what happens in the Composition
• Use LaunchedEffect to call suspending functions • Collect StateFlows as State • Use collectAsStateWithLifecycle on Android -
Thank you, and don’t forget to vote! KotlinConf’23 Amsterdam Márton
Braun @zsmb13 Developer Relations Engineer Google