Is it possible that the scope will be canceled bef...
# compose
n
Is it possible that the scope will be canceled before the `DisposableEffect`'s onDispose is called?
Copy code
val scope = rememberCoroutineScope()
DisposableEffect(Unit) {
    onDispose {
        // do some stuff
        scope.launch { < -- could this be canceled when the call leaves the composition?
            // tear down
        }
    }
}
l
Hmm, why do you need to manually cancel that
z
@Adam Powell Do we make any guarantees about ordering here?
a
so long as the apply dispatcher for the composition is serialized, that
scope.launch
's suspend function won't have an opportunity to run before
scope
is cancelled, but `onDispose {}`'s block does indeed run before that cancellation occurs
n
So I think if I understand what you said correctly, it's best if we do not use the scope from
rememberCoroutineScope
, rather we should use some other scope that has a longer lifecycle.
Copy code
DisposableEffect(Unit) {
    onDispose {
        someLongerScope.launch {
            cameraProvider?.unbindAll()
        }
    }
}
z
If you’re just doing non-suspending cleanup operations that need to run after the frame completes you can pass
NonCancellable
as the context to launch. But that’s dangerous if anything could suspend, since it could leak.
a
If you're just unbinding camerax stuff iirc that doesn't suspend; do it directly in the
onDispose
block and skip the launch entirely