Is there something like `onDispose` for `remember`...
# compose
d
Is there something like
onDispose
for
remember
?
a
not yet but there needs to be. The closest we have so far is
CompositionLifecycleObserver.onLeave
- if
remember
yields an object that implements
CompositionLifecycleObserver
then it'll get the
onLeave
call after a successful composition where it was previously present and then left.
This does not correctly clean up in the event that the composition where the
remember
block was originally called fails, since a resulting
CompositionLifecycleObserver
didn't successfully enter the composition in the first place
for the time being the safest thing to do is make sure
remember
doesn't directly allocate resources that need to be explicitly cleaned up later. Instead,
remember
an object that is safe to GC without explicit disposal and either have that object implement
CompositionLifecycleObserver
to allocate/dispose those resources in `onEnter`/`onLeave` respectively, or use
DisposableEffect(theObject)
to allocate/dispose those resources
(or
LaunchedEffect(theObject)
if suspending semantics make more sense for dealing with cleanup for the particular use case)
d
I've got an image loading cache and I wanted to call
close
to cancel the coroutine scope in it, but it looks like using
rememberCoroutineScope
might be a better approach for the time being.
Thanks for explaining the available options.
👍 1
a
iirc
rememberCoroutineScope
suffers from this gotcha already so when we fix it there you'll inherit the fix 🙃
d
Haha nice.
z
saving this thread for later, thank you!
a
is there an issuetracker ticket to track this that i can follow?
a
Not sure, I'd have to check next week
d
I'm a bit early but does
RememberObserver
solve this completely?
a
yes!
🎉 1
d
Nice!