Kevin Aude
02/23/2021, 10:57 AMMark Murphy
02/23/2021, 12:43 PM<http://Dispatchers.IO|Dispatchers.IO>) and use LaunchedEffect to consume the result.
For example, I have a load() function that gets some data off the Internet. load() is marked with suspend, and the actual network I/O is done on a background thread (courtesy of Retrofit). I then use LaunchedEffect to update a MutableState with the results and use that to drive what to show in a screen:
@Composable
private fun Body(office: String = "OKX") {
var results: MainViewState by remember { mutableStateOf(MainViewState.Loading) }
when (val content = results) {
is MainViewState.Loading -> LoadingState()
is MainViewState.Content -> ContentState(content)
is MainViewState.Error -> ErrorState()
}
val context = LocalContext.current
LaunchedEffect(key1 = office) {
results = load(office, 32, 34, context)
}
}Kevin Aude
02/23/2021, 12:45 PMColton Idle
02/23/2021, 2:18 PMAdam Powell
02/23/2021, 2:28 PMUrlImage(url: String, ...) it's doing something to load that, and if you strip away any intermediate layers of abstraction around caching and testability of the network request/response, it'll probably look a lot like the above.Colton Idle
02/23/2021, 2:31 PMAdam Powell
02/23/2021, 2:32 PMAdam Powell
02/23/2021, 2:35 PMAdam Powell
02/23/2021, 2:36 PMAdam Powell
02/23/2021, 2:38 PMFlow.collectAsState is just a LaunchedEffect that looks a lot like the example above under the hood