2

I'm using MvvmCross to develop my Android app. In that I want to have a Singleton instance of particular ViewModel. For that I tried to implement in similar way as mentioned on this link

Following is my code in my App.cs

public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
    public override void Initialize()
    {
        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        RegisterAppStart(new AppStart());
    }

    protected override Cirrious.MvvmCross.ViewModels.IMvxViewModelLocator CreateDefaultViewModelLocator()
    {
        return MyViewModelLocator ();
    }
}

public class MyViewModelLocator : MvxDefaultViewModelLocator
{
    public MyViewModelLocator()
    {
    }

    public override IMvxViewModel Load(System.Type viewModelType, IMvxBundle parameterValues, IMvxBundle savedState)
    {
        if (viewModelType.GetType() == typeof(CartViewModel))
        {
            var cache = Mvx.Resolve<IMvxMultipleViewModelCache>();
            var cachedViewModel = cache.GetAndClear(viewModelType);

            if (cachedViewModel == null)
                cachedViewModel = base.Load(viewModelType, parameterValues, savedState);

            cache.Cache(cachedViewModel);

            return cachedViewModel;
        }
        else
        {
            return base.Load(viewModelType, parameterValues, savedState);
        }
    }
}

But somehow with this implementation in place, it is not able to get the ViewModel from cache and cache.GetAndClear call always return null inside ViewModelLocator implementation. Apart from this place, I don't call cache from any other place from my code so wonder how come it gets removed from cache.

Also, I was wondering, why I simply cannot make use of IOC (i.e. Mvx.RegisterSingleton<ViewModelName>() and Mvx.Resolve<ViewModelName>()) to get singleton instance instead of cache? Internal implementation of MvvmCross uses cache and not IOC.

Community
  • 1
  • 1
user2185592
  • 368
  • 1
  • 8
  • 24
  • I think IMvxMultipleViewModelCache only gets used for Fragments stuff. So make sure to check IMvxSingleViewModelCache cache as well. But as the name indicates it only caches one VM. – Cheesebaron Sep 17 '15 at 21:04
  • @Cheesebaron I tried to get the my viewmodel instance from IMvxSingleViewModelCache but it doesn't have the instance which I'm looking for. Also, in my code I'm not caching any viewmodels and it is done by mvvmcross framework code only. Instead of using the cache, I used IOC to get my instance (inside VMLocator implementation) and it did return my singleton viewmodel instance. So I wonder should I keep using like that? One question which bothers me is why framework uses cache instead of getting instances from IOC? Is there anyway to modify this behavior? – user2185592 Sep 18 '15 at 05:51
  • Why it is not using IoC to locate ViewModels I don't know. Only the original author knows that. There might be a reason, there might not be. If you feel it should be done that way you are welcome to create a pull request in the MvvmCross repository. You've already discovered the way to modify the behavior. Override methods found in classes and register them instead. I would look here: https://github.com/MvvmCross/MvvmCross/blob/f72a92e8a81b9179d1f75d6214eee8c9ca176221/Cirrious/Cirrious.MvvmCross/ViewModels/MvxViewModelViewTypeFinder.cs – Cheesebaron Sep 18 '15 at 06:52

0 Answers0