Hi guys, I have a weird one ``` fun <T> L...
# coroutines
d
Hi guys, I have a weird one
Copy code
fun <T> LifecycleOwner.stateFlow(stateFlow: StateFlow<T>, funCollect: (T) -> Unit) {
        lifecycleScope.launchWhenStarted {
            stateFlow.collect() {
                funCollect(it)
            }
        }
    }
Is how I subscribe to a Stateflow. I then subscribe to that in my fragment. However, very infrequently, my Stateflow doesn't emit.
Copy code
Timber.d(//This log is hit)
            model.data.emit(result)
The log above is hit, in theory I would think it should have emit, but for some reason, my subscription is never hit in my fragment. The correct object is being pushed to/observed, not sure what's happening, any thoughts?
u
whats that lambda after collect? doesnt seem right
d
It's just a log statement atm
u
does that code compile?
d
Yes
u
oh yea the () is optional lol, nevermind ..
I'd venture the lifecyclescope can be busted, or, since emit is suspending its scope can ran out
and also, since its a mutable flow, it only passes through distinct values (it does a == check inside)
d
Yup. The values are different that are being passed, the lifecyclescope being dead would explain it, but there's no reason it should be
u
try GlobalScope just to prove
d
I did a CoroutineScope(DIspatchers.Main().launch and same issue, still sometimes not receiving updates
u
did you keep it as a field? if not it might get garbage collected
d
The stateflow is a member variable, so I'd think the function would be kept there
r
What is T? If you emit an object that 'equals' the previous value it won't actually do anything.,
Here's an example: https://pl.kotl.in/OXQVvI06w
d
The objects aren't equal