Search code examples
mvvmcross

How to use the DownloadCache plugin?


I am using MvvmCross in my Android project. I have a MvxRecyclerView that contains an image in each item (downloaded from a url), and there are hundreds of items, so I want to use the DownloadCache plugin to help me manage the images/memory so my app doesn't crash with a OutOfMemory exception.

Ideally, when the image/memory limit has been reached, the first image that was downloaded (and is the farthest non-visible item in the MvxRecyclerView) should be freed to make room for the next visible item with its image.

I found the source for it here: https://github.com/MvvmCross/MvvmCross/tree/develop/MvvmCross-Plugins/DownloadCache

How can I use this plugin to achieve this?

EDIT

After trying a few things, I'm stuck over this error that I get when I call Mvx.Resolve<IMvxImageCache<byte[]>>().RequestImage(myImageUrl)

Failed to resolve type MvvmCross.Plugins.DownloadCache.IMvxLocalFileImageLoader

I'm registering the type like this so I don't know why it can't resolve it:

Mvx.ConstructAndRegisterSingleton<IMvxLocalFileImageLoader<Bitmap>, MvxAndroidLocalFileImageLoader>();

I'm registering the IMvxImageCache like this:

Mvx.RegisterSingleton(typeof(IMvxImageCache<byte[]>), () =>
{
  return new MvxImageCache<byte[]>(Mvx.Resolve<IMvxFileDownloadCache>(), 2, 5000000, true);
});

I feel like I'm doing something wrong...


Solution

  • The documentation is available here. The documentation seems to suggest:

    1. Use MvxImageView instead of ImageView.
    2. Use MvxDownloadCacheConfiguration in Setup.GetPluginConfiguration override to customize the cache options as needed.