4

I am working on Xamrin Form right now. I have problem with clear data of ViewModel.

When I logout and login with different user, it shows me data of previous user because the value of UserProfileViewModel doesn't get clear.

When user logout, I want to clear user data from UserProfileViewModel class file. Currently I do this manually when user click on logout. I want any default method like dispose to clear all class member.

I have tried to inherit IDisposable interface with this.Dispose(); but that also didn't work.

I have also tried with default constructor as following but it throws error of

`System.TypeInitializationException`

on this line in app.xaml.cs: public static ViewModelLocator Locator => _locator ?? (_locator = new ViewModelLocator());

public UserProfileViewModel()
{
    //initialize all class member
}

In given code, you can see that on Logout call, I call method

`ClearProfileData` of `UserProfileViewModel` 
which set default(clear) 

data. It is manually. I want to clear data when user logout.

View Model Logout Page

 [ImplementPropertyChanged]
    public class LogoutViewModel : ViewModelBase
    {
        public LogoutViewModel(INavigationService nService, CurrentUserContext uContext, INotificationService inService)
        {
            //initialize all class member
            private void Logout()
            {
                //call method of UserProfileViewModel 
                App.Locator.UserProfile.ClearProfileData();
                //code for logout
            }
        }
    }


User Profile View Model

    [ImplementPropertyChanged]
    public class UserProfileViewModel : ViewModelBase
    {
        public UserProfileViewModel(INavigationService nService, CurrentUserContext uContext, INotificationService inService)
        {
            //initialize all class member
        }

        //Is there any other way to clear the data rather manually?
        public void ClearProfileData()
        {
            FirstName = LastName = UserName = string.Empty;
        }
    }

ViewModel Locator

    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            MySol.Default.Register<UserProfileViewModel>();
        }

        public UserProfileViewModel UserProfile => ServiceLocator.Current.GetInstance<UserProfileViewModel>();
    }

1 Answer 1

4

Firstly there is no need to cleanup these kinds of primitive data types, the gc will do that for you.

However if you use Messages or any other Strong Reference for that matter you WILL have to Unsubscribe from them otherwise your viewmodal will hang around in memory and will never go out of scope

The garbage collector cannot collect an object in use by an application while the application's code can reach that object. The application is said to have a strong reference to the object.

With Xamarin it really depends how you are coupling your View to Viewmodals to determine which approach you might take to cleanup your viewmodals.

As it turns out MVVM Light ViewModelBase implements an ICleanup interface which has an overridable Cleanup method for you.

ViewModelBase.Cleanup Method

To cleanup additional resources, override this method, clean up and then call base.Cleanup().

public virtual void Cleanup()
{
    // clean up your subs and stuff here
    MessengerInstance.Unregister(this);
}

Now your just left with where to call ViewModelBase.Cleanup

You can just call it when your View Closes, if you get a reference to the DataContext (I.e ViewModalBase) on the DataContextChanged Event

Or you can wire up a BaseView that plumbs this for you, or you can implement your own NagigationService which calls Cleanup on Pop. It really does depend on who is creating your views and viewmodels and how you are coupling them

2
  • 1
    Thanks you for such a detailed description. I have tried to put public virtual void Cleanup() in UserProfileViewModel and call this method on Logout click of LogoutViewModel. But this didn't help. Can you please suggest me if I place Cleanup method on wrong place? I am new in Xamrin.
    – Developer
    Feb 16, 2018 at 10:14
  • 1
    It's done Michael. It was my mistake. I have replaced my instance instead of MessengerInstance and it's working. Thank you very much.
    – Developer
    Feb 16, 2018 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.