sebaslogen / resaca

Android library to scope ViewModels to a Composable, surviving configuration changes and navigation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ViewModel.onCleared() is not called

gmk57 opened this issue · comments

Thanks for this library, it might be very helpful. 👍

Small gotcha: ScopedViewModelContainer.scheduleToDispose() calls viewModelScope.cancel(), but does not call clear() or onCleared() on scoped ViewModels. It think it should, if possible (these methods are not public, unfortunately).

Good point, I´ll take a look at a Java workaround to call protected methods.
Thanks for the suggestion.

Fixed and released as hotfix version 1.1.1 https://github.com/sebaslogen/resaca/releases/tag/1.1.1

Hmm, I've added some logs to FakeScopedViewModel, navigated back and forth, but onCleared wasn't called.

It seems ScopedViewModelContainer only calls ViewModelClearer in scheduleToDispose, not in its own onCleared.

In my fist message, I missed the fact that there are two "cleanup" places, unfortunately.

BTW, maybe it's better to extract common logic, to handle is CoroutineScope and is CoroutineContext cases too?

Hmm, I've added some logs to FakeScopedViewModel, navigated back and forth, but onCleared wasn't called.

It seems ScopedViewModelContainer only calls ViewModelClearer in scheduleToDispose, not in its own onCleared.

In my fist message, I missed the fact that there are two "cleanup" places, unfortunately.

BTW, maybe it's better to extract common logic, to handle is CoroutineScope and is CoroutineContext cases too?

Thanks for finding this bug.

Yes, I was thinking about the same solution, the logic should be all moved to a single clean up method and I'll add support for the Closeable interface like ViewModel does.

I'll also add some tests to make it more robust.

I've fixed the points above and added a test. Let me know what you think and if you don't find anything I'll create a new release.

Looks good to me. :)
Probably this README section should be updated too.
I wish Google and JetBrains fixed their bugs that fast... ;)

Wonderful, thanks for the doc update tip, you're 💯 awesome

Release https://github.com/sebaslogen/resaca/releases/tag/1.1.2 closes this ticket