Question regarding new androidx lifecycle (2.4.0) ...
# orbit-mvi
o
Question regarding new androidx lifecycle (2.4.0) and the article https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda Isn’t the proper way to bound to the lifecycle is like?
Copy code
@Composable
fun <STATE : Any, SIDE_EFFECT : Any> ContainerHost<STATE, SIDE_EFFECT>.collectAsState(
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED
): State<STATE> {
    val lifecycleOwner = LocalLifecycleOwner.current
    val lifecycleAwareStateFlow = remember(this, lifecycleOwner) {
        container.stateFlow.flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState)
    }
    return lifecycleAwareStateFlow.collectAsState(currentState)
}
1
k
Mikolaj or Matt can correct me if I am wrong. My understanding is the
flowWithLifecycle
works only in case of cold
Flow
, where the repeat on lifecycle can cancel the `Flow`'s event generator. --- Blogpost example -- Looking at the example in the post they accept a cold flow stream that provides Location data.
Copy code
@Composable
fun LocationScreen(locationFlow: Flow<Flow>) 
...
Unsubscribing from
locationFlow
while compose is in the background might reduce the pressure on location services and we can resubscribe when the app comes to the foreground. --- ContainerHost --- ContainerHost contains a
StateFlow
which is hot and accepts state regardless the number of subscribers connected to the `StateFlow`'s instance. Adding a repeat won't cancel the generating source as it is providing states to the
stateFlow
.
o
@kioba basically you’re right as the main point of the new API is to cancel the underlying producer properly, but also, one more thing is about effects -> as the collect coroutine is now just suspended and not canceled, there is a chance to miss effects into the void during the resume (e.g. view destroy)
k
sorry, I haven't looked at the SideEffects before, It might be the case but I am not exactly sure how you mean missing an effect as the collector suspended.
o