I was wondering if there is a way to null binding contexts of all viewmodels and then garbage collect on logout? In my application
async void OnLogoutClicked(object sender, EventArgs args)
{
try
{
var result = await UserLogoutRequest.Logout();
if (result.responseCode == ResponseCodes.SUCCESS)
{
DialogService.ShowSuccessToast("Logout successful");
}
else
{
DialogService.ShowErrorToast("A server error occurred during logout");
}
}
catch (Exception ex)
{
DialogService.ShowErrorToast("A client error occurred during logout");
}
finally
{
await DataManager.Instance.SetLoggedInUser(null);
await DataManager.Instance.SetToken(string.Empty);
GC.Collect(); // TODO: Figure out how to GC the ViewModels so that when user logs in again it's not using the same instance of the VM
await AppShell.Current.GoToAsync("//Welcome");
}
}
}
The GC.Collect() does not seem to do much since the pages are still linked to their viewmodels. I guess I could do BindingContext = null in the OnDisappearing of the pages, but I feel that the OnDisappearing gets called too often. I would prefer to just null the BindingContext of all pages on logout and then garbage collect, but there doesn't seem to be any hook for me to do that.
Thanks in advance.