Fix: SnackbarPresenter might throw ObjectDisposedException, display problems.
Pull request type
Please check the type of change your PR introduces:
- [ ] Update
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
What is the current behavior?
-
In a scenario where a
SnackbarPresenteris utilized within a page that employs a singleton service and is treated as aNavigationViewItem, switching between NavigationViewItems can repeatedly invoke theOnUnloadedmethod. This sequence leads to theCancellationTokenSourcebeing disposed via itsDisposemethod, followed by attempts to callCancelmethod on the already-disposed object, consequently resulting in anObjectDisposedExceptionbeing thrown. -
During the process of displaying a
Snackbar, if the user navigates to another page, theSnackbarfails to dismiss properly.
Issue Number: Snackbar Presenter throws an error when it is unloaded. #951
What is the new behavior?
The CancellationTokenSource will no longer be disposed within the OnUnloaded method; rather, its disposal will be deferred to the class's finalizer. Additionally, invoking the OnUnloaded method will promptly trigger the hiding of the Snackbar.
I'm unsure if there's a better approach to properly dispose the CancellationTokenSource, as relying on the finalizer could result in untimely disposal due to the inherent unpredictability of garbage collection.