Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure MvvmCross to persist cached images

This is the question that's following the unanswered one Image Caching from http sources.

I used the code from samples of MvvmCross and I was able to run MvxImageViewLoader with my project. It caches the images for current instance of the application, but if I restart it all the images are being reloaded again. I would like to keep them persistent in iPhone's Library\Caches folder.

I noticed that in the application log there is a line:

mvx: Warning:   2.25 Persistent download cache will not be available - no text serializer available

It comes from framework's MvxFileDownloadCache class because IoC container doesn't have a registered class for IMvxTextSerializer interface. So I assume that if I define my implementation for this interface then the solution with permanent caching will work somehow. Although the nature of the interface seems suspicious to be used for image saving to a disk since it operates with objects to serialize and strings to deserialize. So I am actually confused and can't realize which way to go.

like image 905
Eugene Tiutiunnyk Avatar asked Mar 24 '23 03:03

Eugene Tiutiunnyk


2 Answers

As answered in my answer to the unanswered question, showing images on wp, winrt, xamarin.android and xamarin.touch from http is shown in these two N+1 videos:

  • Kittens - N=2 - https://www.youtube.com/watch?v=e-ZKgO8fTw4
  • Books - N=6 - https://www.youtube.com/watch?v=He6QvnLsPUA

The second of these downloads and persists images across sessions using the plugin nuget packages:

  • MvvmCross.HotTuna.Plugin.DownloadCache
  • MvvmCross.HotTuna.Plugin.File
  • MvvmCross.HotTuna.Plugin.Json

I am actually confused and can't realize which way to go.

Obviously MvvmCross' download code is only one implementation - and there is some evidence that Mono-httpwebrequest-based download is not 100% reliable - see MvxDynamicImageHelper unreliable. Users are free to implement their own image download plugins - e.g. using iOS code like from iOS: Download image from url and save in device.

like image 119
Stuart Avatar answered Apr 20 '23 03:04

Stuart


Well, apparently the issue was really simple to solve. You just have to make sure that your ios project references Cirrious.MvvmCross.Plugins.Json library from MvvmCross libs set (you'll have to build it with the link to Newtonsoft.Json.dll). Also don't forget to register the plugins in your application. Basically this is the config that gives your caching and persisting of all MvxImageViewLoader's loaded images:

public class Setup : MvxTouchSetup
{
    public Setup (MvxApplicationDelegate appDelegate, IMvxTouchViewPresenter presenter)
        : base(appDelegate, presenter)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        return new YourAppClass();
    }

    protected override void AddPluginsLoaders(MvxLoaderPluginRegistry registry)
    {
        registry.AddConventionalPlugin<Cirrious.MvvmCross.Plugins.DownloadCache.Touch.Plugin>();
        registry.AddConventionalPlugin<Cirrious.MvvmCross.Plugins.File.Touch.Plugin>();

        base.AddPluginsLoaders(registry);

    }

    protected override void InitializeLastChance ()
    {
        Cirrious.MvvmCross.Plugins.DownloadCache.PluginLoader.Instance.EnsureLoaded();
        Cirrious.MvvmCross.Plugins.File.PluginLoader.Instance.EnsureLoaded();
        Cirrious.MvvmCross.Plugins.Json.PluginLoader.Instance.EnsureLoaded();

        base.InitializeLastChance();
    }

}

My initial observation regarding the console log line that there is no text serializer was a key to solve the problem. I looked into my app Caches folder in iPhone Simulator and found tons of cached images in Pictures.MvvmCross folder, but according to the source code of MvvmCross framework there also must be Pictures.MvvmCross_CacheIndex.txt with serialized list of Entities to give the caching engine the way to load the list of existing image files. Due to missing serializer this list file wasn't created and hence on the next application restart all the info about existing loaded images was lost.

like image 22
Eugene Tiutiunnyk Avatar answered Apr 20 '23 01:04

Eugene Tiutiunnyk