Tips for Offline in Azure Mobile Services on Xamarin.iOS

To Offline or not to Offline? On one hand it adds a fair amount of code complexity but in those times when a user is doing a very important task and a connection is lost they’re not going to be very happy when their app just stops working.

In wanting a high quality product I’ve been on the search for ways to support offline in my app. I discovered that the Azure Mobile Services team had Offline support in their client library.

The deep dive tutorial gives a good overview of the Offline support. At the moment the support it targeted towards Windows Mobile so there’s a few tricks to get it working on Xamarin.iOS. I’m going to go over the steps I had to take to get it working in a simple in Xamarin.iOS project in Xamarin Studio, in some parts I have to jump over to Visual Studio because the nuget support in Xamarin studio is a few versions behind.

You can check out the Offline in Azure Mobile Service deep dive tutorial here.

There is a azure mobile services package in the Xamarin Component Store but at the moment you actually need the prerelease version. So to get it working you need to use nuget from Visual Studio, personally I had to get from the component store and copy the assembles from another project to override the component store assemblies but you *should* just be able to open the project in visual studio and install the prerelease package. Installing this from the package manager in Xamarin Studio it  included:

  • Microsoft.WindowsAzure.Mobile.Ext
  • Microsoft.WindowsAzure.Mobile
  • Newtonsoft.Json
  • System.Net.Http.Extensions
  • System.Net.Http.Primitives
  • System.Net.Http

The second package you need is the SQLiteStore which is also prerelease and also needs to be downloaded from visual studio.

The third package/assembly you’ll need is a SQLitePCL that works with Xamarin.iOS, thankfully someone has already been working on it and added a pull request that add Xamarin support to SQLitePCL. https://sqlitepcl.codeplex.com/SourceControl/network/forks/mattleibow/SQLitePCL/contribution/6607
You’ll need to clone this then add a reference to the SQLitePCL project and the SQLitePCL.Ext.iOS project, I had to change the SQLitePCL to profile78 to get it working.

One other issue I encountered was that the SQLitePCL couldn’t load the iOS platform specific code, the reason for this is that the assembly wasn’t used by any project directly (it’s loaded via Reflection) so it didn’t load the assembly.

So in the Application.Main you just need to reference the provider for the extension into a dummy variable.

1
var platform = SQLite3Provider.Instance;

 

Leave a Reply