Microsoft Azure Mobile Services Managed Client SDK with Offline Support

In this post, I will take a look at the new offline support provided by Microsoft Azure Mobile Services to the managed client SDK v1.3 alpha, which can be used for building Windows store apps with offline capabilities and syncing data between local database and Azure Mobile Service database. Microsoft Azure Mobile Services has been evolving over time with new useful features. On the last update, Azure Mobile Services started to support .NET for writing backend so that developers can now write backend logic either in C# or in JavaScript/Node.js. The latest addition to the evolving Microsoft Azure Mobile Services is the support for working with offline scenarios, which is an excellent feature, while we are building mobile apps. This lets the mobile apps to store data on the local storage in offline scenario and can sync-up the data to the Mobile Services storage when we are connected to the internet. The current version is a preview version available for managed clients. SQLite is used for persisting the offline data on the client apps.

Setting up the Mobile Services client with offline data persistence

The following steps demonstrate how to set up a Windows Store client app for working with offline storage on SQLite and sync offline data with Azure Mobile Services data.

Step 1 – Install SQLite for Windows 8.1

SQLite is used for persisting the data in the offline scenario so download and install SQLite for Windows 8.1 from here.

Step 2 – Add reference to SQLite for Windows Runtime (Windows 8.1)

In your Windows Store app, add a reference to SQLite for Windows Runtime (Windows 8.1) from Add Reference, Reference Manager, choose Extensions from Windows as shown in the below figure.

image

Step 3 – Add NuGet package WindowsAzure.MobileServices.SQLiteStore

Add NuGet package WindowsAzure.MobileServices.SQLiteStore in order to working with SQLite store in the offline scenario. Please keep in mind that you should select “Include Prerelease” to get the alpha version of the package.

image

Step 4 – Define Data Models with system property Version

In order to working with data syncing scenarios, data models should include a system property Version. You can decorate a version property with MobileServices.VersionAttribute. 

public class TodoItem
    {
        public string Id { get; set; }
 
        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }
 
        [JsonProperty(PropertyName = "complete")]
        public bool Complete { get; set; }
 
        [Version]
        public string Version { get; set; }
    }

Step 5 – Declare a IMobileServicesSyncTable Table for data sync

Define the IMobileServicesSyncTable table object to working with a model entity for data sync scenarios.

//using statement for IMobileServiceSyncTable<T> 
using Microsoft.WindowsAzure.MobileServices.Sync;
 
 private IMobileServiceSyncTable<TodoItem> todoTable = App.MobileService.GetSyncTable<TodoItem>();

Step 6 – Initializes the client sync context with a SQLite store

Initialize the client sync context object with a SQLite data store.

//Using statement for SQLiteStore
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
 
if (!App.MobileService.SyncContext.IsInitialized)
{
    var store = new MobileServiceSQLiteStore("localsync12.db");
    store.DefineTable<TodoItem>();
    await App.MobileService.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
}

In the above code block, MobileService object is the Mobile Services client of your Windows Store App.

public static MobileServiceClient MobileService =
            new MobileServiceClient(
    "https://shijuvar-zumo.azure-mobile.net/",
    "your-application-key"
);

Data sync between offline storage and Mobile Services Data

In the above steps, we have configured our Windows store app for working with offline storage in a SQLite database. The information below provides how to insert data into offline storage and sync the data between offline and Azure Mobil Services storage.

Insert data to offline storage

The code block below inserts a new  data to the database

private async void InsertTodoItem(TodoItem todoItem)
{            
    await todoTable.InsertAsync(todoItem);           
}

Push data to Azure Mobile Services storage from offline database

The code block below push the data to Azure Mobile Services from offline storage

private async void Push()
{
    string errorString = null;
 
    try
    {
        await App.MobileService.SyncContext.PushAsync();
        
    }
    catch (MobileServicePushFailedException ex)
    {
        errorString = "Push failed because of sync errors: " + ex.PushResult.Errors.Count() + 
            ", message: " + ex.Message;
    }
    catch (Exception ex)
    {
        errorString = "Push failed: " + ex.Message;
    }
 
    if (errorString != null) {
        MessageDialog d = new MessageDialog(errorString);
        await d.ShowAsync();
    }
}

Pull data to local database from Azure Mobile Services storage

The code block below pull the data from Azure Mobile Services storage to local database which is used as offline storage.

The following MSDN code samples provides reference on the new Azure Mobile Services offline support.

Summary

Microsoft Azure Mobile Services has been evolving with lot of new features which lets the Mobile Services developers to develop compelling mobile apps. The new offline support is a very useful feature while building mobile apps . In this post, we have configured our Windows Store app to working with an offline storage in SQLite database. The Mobile Services client SDK provides API for syncing data between offline database and Mobile Services database.

You can follow me on Twitter @shijucv

1 Comment

  • Do you know if the source code of the SQLiteStore is available?
    It could be interesting to extract this library from azure to use it also as a common local cache/offline database.
    The MobileServiceSQLiteStore seems to be injected to the azure Service. So maybe it can be used as a standalone library.
    I like the MobileServiceSQLiteStore is using the Sqlite PCL but seems to be generic ("store.DefineTable();")

Comments have been disabled for this content.