Xamarin.Forms: Writing to Disk With Akavache

To recap, I’m writing a shopping cart app for Windows Phone, Android, and iOS.  The purpose of the app is primarily to let me use Forms.  Each post will build on top of the previous one.

Last time I added behaviors to my XF xaml.  This week I’m going to save settings to disk using Akavache.

Once I started writing the post to go along with this week’s post, I quickly became aware that the it was longer than what really makes sense for a single post.  So I’m splitting the article in two.  This week will be logging in, next week will be logging out.  Both weeks will use the same code.  For consistency’s sake, I’ll be creating a new release for next week, even though it will point to the same code.

Recap and Code

This is the eight post in the series, you can find the rest here:

  • Day 0:  Getting Started (blog / code)
  • Day 1:  Binding and Navigation (blog / code)
  • Day 2:  Frames, Event Handlers, and Binding Bugs (blog / code)
  • Day 3:  Images in Lists (blog / code)
  • Day 4:  Search and Barcode Scanner (blog / code)
  • Day 5:  Dependency Injection (blog / code)
  • Day 6:  Styling (blog / code)
  • Day 7:  Attached Behaviors (blog / code)
  • Day 8:  Akavache (blog / code)

The latest version of the code can always be accessed on the GitHub project page.

Installing Akavache Cheat Sheet

At the risk of spoiling the narrative below, here’s a very brief outline of the steps to install and use Akavache in a Xamarin Forms app.  For links and more details, continue reading.

  1. Install Akavache package in common PCL project (Install-Package akavache)
  2. Install Akavache package in platform specific projects
    1. Droid
    2. Win Phone
    3. iOS*
  3. WinPhone: Update Splat package (Update-Package splat)
  4. WinPhone:  Change build configuration to x86
  • The iOS step is assumed to be necessary and sufficient to make Akavache work, but I am unable to verify it.

Logging In

From the beginning one of the things that really bothered me about this app is that I have to log in each time I use it.  And that’s including the fact that I don’t have to type in a particular username/password.  Any text will do.  I guess I’m just lazy.

To persist logging in, I’m just going to write a little bit of information to disk when I’m logging in for the first time.  Next time I open the app, I’ll just check for that value, if it exists I’ll go straight to the main landing page.

I don’t want to tie my implementation down to an unknown framework, so the first thing I need to do is create an interface to abstract away persisting data:

public interface ICache
{
    Task<T> GetObject<T>(string key);
    Task InsertObject<T>(string key, T value);
    Task RemoveObject(string key);
}

This interface should handle all of m CRUD operations.  Right now the only person that needs to use it is my LoginService.

public async Task<User> LoginAsync(string username, string password)
{
    User user = Login(username, password);

    await _cache.InsertObject("LOGIN", user);

    return user;
}

public async Task<bool> IsLoggedIn()
{
    User user = await _cache.GetObject<User>("LOGIN");
    return user != null;
}

LoginAsync performs the standard log in check, verifying that username and password are valid credentials (in reality it just checks that they are non-null) and returns a user object if they are valid.  I then save that in the cache using “LOGIN” as the key.  Next, IsLoggedIn in checks to see if there is a non-null value stored in the cache.

Now that we know how the cache is going to be used, let’s implement it.

Akavache

I’ll be using Akavache as my data store.  It’s probably a bit overkill for the very simple use case I have but I have a few reasons why I want to use it.  First, I really don’t want to think about where exactly I’m storing this data.  It could be in a database, it could be on disk, my app doesn’t really care.  It’s just a set of keys and values to me.  While Akavache does use a SQLite backend, I don’t have to know any of the details.  I’m also looking for something that is async/await friendly.  Akavache supports many platforms, I’d love to be able to write this once for all platforms.  And finally, I recently heard about it and kind of wanted to play with it.

Here’s a summary of what I just stated above as my reasons for using Akavache

  • Abstract away storage details
  • async API
  • Cross platform
  • It’s new to me

Akavache on Droid

Akavache is a nuget package, so installation is easy.  I’m installing the package into the core project (ShoppingCart.csproj).  For it to work it also needs to be installed on the Droid project as well.  In my Services folder I’ll add my Akavache implementation of ICache.  This class will be used by all of the platforms.

using Akavache;
using ShoppingCart.Services;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace ShoppingCart.Services
{
    public class Cache : ICache
    {
        public Cache()
        {
            BlobCache.ApplicationName = "ShoppingCart";
        }

        public async Task RemoveObject(string key)
        {
            await BlobCache.LocalMachine.Invalidate(key);
        }

        public async Task<T> GetObject<T>(string key)
        {
            try
            {
                return await BlobCache.LocalMachine.GetObject<T>(key);
            }
            catch (KeyNotFoundException)
            {
                return default(T);
            }
        }

        public async Task InsertObject<T>(string key, T value)
        {
            await BlobCache.LocalMachine.InsertObject(key, value);
        }
    }
}

First thing to note here is that I’m importing the System.Reactive.Linq namespace.  This enables us to await the Akavache calls.  Next thing to note is that I’m setting the ApplicationName property in the constructor.  This is needed to initialize storage.  The rest of the implementation is pretty straight forward.  You can even see that I got most of my method names from the Akavache API.

The RemoveObject implementation invalidates the object in Akavache.  GetObject tries to read the object from the cache, catching any KeyNotFoundExceptions.

Next week I’ll go over how all of this gets plugged into the UI.

Akavache on Win Phone

Akavache is a bit more fiddly on Win Phone.

To start off, install the Akavache nuget package directly in the Win Phone project.  Remember, it’s already installed in my PCL project where it is being used, it needs to be installed here as well, even though none of the code reference it directly.  At this point, I get my first error:

System.TypeInitializationException was unhandled by user code
  HResult=-2146233036
  Message=The type initializer for 'Akavache.BlobCache' threw an exception.
  Source=Akavache
  TypeName=Akavache.BlobCache
  StackTrace:
       at Akavache.BlobCache.set_ApplicationName(String value)
       at ShoppingCart.WinPhone.Services.WinPhoneCache..ctor()
       at lambda_method(Closure , Object[] )
       at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
  InnerException: System.IO.FileLoadException
       HResult=-2146234304
       Message=Could not load file or assembly 'Splat, Version=1.4.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
       Source=Akavache
       StackTrace:
            at Akavache.BlobCache..cctor()
       InnerException:

There is an assembly that can’t be found.  Splat is a another package produced by Paul Betts (the producer of Akavache).  The problem here is not that the file isn’t there, but that it’s an older version.  All I need to do is update the nuget package for Splat and I’m good.

Updating Splat

With this resolved, the next exception I get is that correct version of SQLitePcl.Raw is missing.

System.TypeInitializationException was unhandled by user code
  HResult=-2146233036
  Message=The type initializer for 'Akavache.Sqlite3.Internal.SQLiteConnection' threw an exception.
  Source=Akavache.Sqlite3
  TypeName=Akavache.Sqlite3.Internal.SQLiteConnection
  StackTrace:
       at Akavache.Sqlite3.Internal.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
       at Akavache.Sqlite3.SQLitePersistentBlobCache..ctor(String databaseFile, IScheduler scheduler)
       at Akavache.Sqlite3.Registrations.<>c__DisplayClass6.<Register>b__2()
       at System.Lazy`1.CreateValue()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Lazy`1.get_Value()
       at Akavache.Sqlite3.Registrations.<>c__DisplayClass6.<Register>b__3()
       at Splat.ModernDependencyResolver.GetService(Type serviceType, String contract)
       at Splat.DependencyResolverMixins.GetService[T](IDependencyResolver This, String contract)
       at Akavache.BlobCache.get_UserAccount()
       at ShoppingCart.Services.LoginService.get_Users()
       at ShoppingCart.Services.LoginService.<LoginAsync>d__4.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ShoppingCart.ViewModels.LoginViewModel.<Login>d__0.MoveNext()
  InnerException: System.IO.FileNotFoundException
       HResult=-2147024894
       Message=Could not load file or assembly 'SQLitePCL.raw, Version=0.5.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
       Source=Akavache.Sqlite3
       FileName=SQLitePCL.raw, Version=0.5.0.0, Culture=neutral, PublicKeyToken=null
       StackTrace:
            at Akavache.Sqlite3.Internal.SQLiteConnection..cctor()
       InnerException:

Again this is a nuget package, but this time updating does not fix the problem.  I reached out to Paul about the issue and he told me that I have to change the build configuration of the Win Phone project to specify x86 as my platform.

Open Configuraton Manager

Configuraton Manager

Now running the app will work.

Thanks

A big thanks to Paul Betts both for writing Akavache and for providing the last mile of help when I needed it.

Happy Coding

This entry was posted in Uncategorized by Josh. Bookmark the permalink.

About Josh

I signed up for a BASIC class in junior high. That got me hooked and pretty soon I started experimenting with harder languages. My college years are a blur of benders hopped up on Perl and Java and that one semester where I sunk so low that I was mainlining Smalltalk. Now I'm a complete C# junky living it one day at a time. Hello, my name is Josh.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>