Update Fragment ViewModel caching to prevent stale reuse and memory retention #4958
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)
Bug fix
Fixes #4913
When a ViewModel of a specific type is already present in the cache, MvvmCross prevents caching a second instance of the same type until the first one is explicitly removed. This results in scenarios where the previously cached instance is returned, even though a new instance was intended. Consequently, the new ViewModel is not cached and may not be retrieved as expected. Additionally, the retained ViewModel remains in memory longer than necessary, which can interfere with garbage collection and lead to unintended memory retention.
🆕 What is the new behavior (if this is a feature change)?
When a ViewModel of a specific type is already cached and a new instance of the same type is requested to be cached, the new instance will now replace the existing one in the cache. This ensures that the most recent ViewModel is always retrievable and avoids unintended reuse of stale instances.
Additionally, the cached ViewModel is now explicitly removed during the OnResume lifecycle event. Since MvvmCross only attempts to retrieve cached ViewModels during OnCreate, retaining them beyond that point serves no purpose and may lead to unnecessary memory retention. This change helps reduce memory pressure and improves lifecycle alignment.
💥 Does this PR introduce a breaking change?
No
🐛 Recommendations for testing
Verify ViewModel replacement in cache:
ViewModeltype.ViewModelreturns the most recent instance.Validate cache clearing on OnResume:
OnResume.ViewModelis removed from the cache.Note on bug [Bug]: Stale ViewModel is sometimes returned from MvxMultipleViewModelCache in Android #4913 and [Bug]: Regression: typeof(TViewType) in MvxViewExtensions breaks ViewModel resolution #4957:
📝 Links to relevant issues/docs
Bug [Bug]: Stale ViewModel is sometimes returned from MvxMultipleViewModelCache in Android #4913
This PR addresses the caching issue described here. The issue includes a minimal repro project and testing instructions.
Bug [Bug]: Regression: typeof(TViewType) in MvxViewExtensions breaks ViewModel resolution #4957
This newer issue interferes with the ability to fully verify the fix for [Bug]: Stale ViewModel is sometimes returned from MvxMultipleViewModelCache in Android #4913. Reviewers should be aware that the repro steps from [Bug]: Stale ViewModel is sometimes returned from MvxMultipleViewModelCache in Android #4913 may not work as expected until [Bug]: Regression: typeof(TViewType) in MvxViewExtensions breaks ViewModel resolution #4957 is resolved.
🤔 Checklist before submitting