Kevin Worth
03/08/2023, 4:51 PM@Composable
fun MyModelScreen(modifier: Modifier = Modifier, viewModel: MyModelViewModel = hiltViewModel()) {
val lifecycle = LocalLifecycleOwner.current.lifecycle
val items by produceState<MyModelUiState>(
initialValue = MyModelUiState.Loading,
key1 = lifecycle,
key2 = viewModel
) {
lifecycle.repeatOnLifecycle(state = STARTED) {
viewModel.uiState.collect { value = it }
}
}
if (items is MyModelUiState.Success) {
MyModelScreen(
items = (items as MyModelUiState.Success).data,
onSave = viewModel::addMyModel,
modifier = modifier
)
}
}
how would one “restart” the collection if/when uiState
runs into a catch
which cancels the subscription? In my case uiState
is (slightly different from the template), something like flowA.flatMapLatest {...flowB...}.map { Success(it) }.catch { emit(Error(it)) }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), Loading)
(though, for the sake of the question, I’m not really sure that detail matters…)
I’ve tried things like displaying an error with an “Ok” button, and the button modifies the value of key1
or key2
(say, for instance, one of the keys is an Int
which gets incremented when you press the “Ok” button), but no joy.Zach Klippenstein (he/him) [MOD]
03/08/2023, 5:02 PMKevin Worth
03/08/2023, 5:10 PMretry
before the catch
and yes, that can work. But, the problem is you (can) either loop forever (don’t limit the number of retries) or eventually end up in the catch
and you’re back to being out of luck.retry
really felt like it was good when seeing the app run as expected, but it felt gross when it came to running the tests and realizing it was just looping forever.flatMapLatest
is already inherently a long running loop (listener in a coroutine), and so maybe the retryWhen
getting called over and over isn’t a bad thing…? Is that accurate? …maybe a question for #coroutines and/or #flow?Zach Klippenstein (he/him) [MOD]
03/08/2023, 8:16 PMKevin Worth
03/08/2023, 8:58 PMretry
I set state which is used above it (in say the flatMapLatest
) then it won’t keep looping. Something like, isReady
or something. Set that to false, have that return an Error
result (separate from the one in the catch
), and I should be good to go, I think.
Thanks!Zach Klippenstein (he/him) [MOD]
03/08/2023, 9:02 PM