Adding Offline Sync to an Azure Mobile Apps Universal Windows App

I added a data sync capability to my Universal Windows App last time around. This is good, but there are a few problems with it. I mentioned a few of them when I concluded the last article. I’m going to concentrate on a main problem in this article and suggest a mechanism by which you can solve it. TL;DR – you want to use Offline Sync to get around performance as well as network connectivity issues.

When you log on with the application provided in uwp-0.6, there is a delay and it’s relatively significant. It will get worse as you get more and more records. We alleviate the problem somewhat by having a local cache of the current set of data. The cache is renewed when you log in. What about a mobile device though? Firstly, you can’t always count on a connection to the Internet, so logging in and retrieving the current state may not be possible. I regularly go “off the net” when I am travelling abroad, for example. Secondly, there may be a ton of data there – do you really want to transfer all that data each and every time you log in? Mobile providers are tying profits to bandwidth usage, so you want to minimize your bandwidth usage to be a good app. For all these reasons, implementing a local sync cache that is persistent is just a good idea.

Fortunately, the Azure Mobile Apps .NET Client SDK provides for an offline sync capability built on top of SQLite. I’m going to adjust the TaskStore.cs file to handle this today. To start, ensure you install the SQLite for Universal App Platform extension – you can install it from the Extensions and Updates dialog in the Tools menu:


You should also add the NuGet Package WindowsAzure.MobileServices.SQLiteStore – ensure you get the 2.0.0-beta release (or later). Finally, ensure you add a reference to the SQLite for Universal App Platform to your project. Right-click on the References and select Add Reference… Select the Extensions list and place a check-mark next to SQLite for Universal App Platform:


All the other changes are in the TaskStore.cs. First, some new references:

// Offline Sync Requirements
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;

We also need a mechanism to create the local SQLite store and sync the database. I do this in the same method:

        private async Task SynchronizeStoreAsync()
            if (!App.MobileService.SyncContext.IsInitialized)
                System.Diagnostics.Debug.WriteLine("Creating local cache context");
                var store = new MobileServiceSQLiteStore("taskstore.db");
                await App.MobileService.SyncContext.InitializeAsync(store);
            System.Diagnostics.Debug.WriteLine("Pushing changes to remote server");
            await App.MobileService.SyncContext.PushAsync();
            System.Diagnostics.Debug.WriteLine("Pulling changes from remote server");
            await tableController.PullAsync("taskItems", tableController.CreateQuery());

Finally, each of the methods (Create, Update, Delete, Refresh) needs to call this function at an appropriate time – after the write or before the read. For example, here is the Create method:

        public async Task Create(TaskItem item)
            item.Id = Guid.NewGuid().ToString();
            if (User != null)
                System.Diagnostics.Debug.WriteLine("Inserting item into remote table");
                await tableController.InsertAsync(item);
                await SynchronizeStoreAsync();
                System.Diagnostics.Debug.WriteLine("Inserted item into remote table");

When the first synchronization event happens, the local SQLite database file will be created in ~/AppData/Local/Packages/_something_/LocalState. You can find it using PowerShell:


I use the SQL Server Compact/SQLite Toolbox extension for Visual Studio to access the contents of the database. Once installed, you can access it through the Tools menu. To add your SQLite database, right-click on the Data Connections and select Add SQLite Connection… Click on Browse and find your taskstore.db file. Click on Close and your file will be added to your Data Connections. You can expand it and see the tables.

Note that there are three tables that are not yours – the main one you should be concerned with is __operations. This contains a list of changes that have not been pushed to the remote table yet. In normal operation this should be empty. However, it will contain the “to be sent” records when you are off-line.

You can get the code for this update from my GitHub Repository.


I did this project the wrong way round. I thought I was being clever by putting together a local cache then added data sync and then offline sync. In reality, I should have written my store based on the offline sync cache and then added the online code later on. In that way, my store would have been always available and then sync to the cloud would be an optional extra. I’m definitely doing that for the future.

I also need to get a generic filtering and sorting capability. I’m thinking of a hierarchy where the store is the lowest level and it emits an CollectionChanged event (i.e. it implements the INotifyCollectionChanged interface). The FilteredCollection<> class will wrap that collection and do the filtering on the object via predicates, emitting a CollectionChanged event only if something changed. Finally, the SortedCollection<> class will wrap that collection and do the sorting for me. By doing it as three separate classes, I get the advantage of handling those elements completely separately and I can still work within the Universal App Platform restrictions. That, however, is for another time.