Is this perhaps already a standard thing? ```@Com...
# compose
j
Is this perhaps already a standard thing?
Copy code
@Composable
fun <T> rememberAsyncSaveable(vararg keys: Any?, block: suspend CoroutineScope.() -> T): State<Result<T>?> {
    val state = rememberSaveable(*keys) { mutableStateOf<Result<T>?>(null) }
    if (state.value == null) {
        LaunchedEffect(state) {
            state.value = runCatching { block() }
        }
    }
    return state
}
(Modulo probably needing a custom saver.)
e
same effect and easier as
Copy code
remember(*keys) { flow { emit(block()) } }.collectAsState(null)
I think
j
That's not saved though
The unsaved case is pretty well covered.
Also the effect job isn't a supervisor, so exceptions in your example cause the entire recomposition to fail.
e
exceptions shouldn't be handled in your ui layer anyway (and with flow you could just
.catch {}
them to ignore) but ok, if the savable part is important to you…
j
Well, in my case the block is an expensive network operation. Would much rather not redo it on activity recreate.
This might be something that should be in the view model but I'm still struggling with how to get async stuff to work there.
e
there's a platform limit on savedInstanceState size and if you're too large, your app will end up killed in the background by binder TransactionTooLargeException; for network calls I'd generally define a repository to cache the values to disk
j
Hmm
This one's not big but that's a good call out.