• 2 min read

Offline Sync for Mobile Services

The best mobile apps handle network interruptions gracefully, allowing users to be productive despite the lack of connectivity. Learn how you can use the offline sync feature of Azure Mobile Services to create responsive apps that work, even when the network doesn't.

When writing modern mobile apps, developers have to consider the reality that end users may not always have network access. This can be due to a transient network issue, or it could be a mobile app that’s often used in remote areas with little connectivity.

To make it easier to write these kinds of apps, the Azure Mobile Services SDK offers a preview feature for syncing your backend data with a local database on the device. The feature is available on both the JavaScript and .NET backends, and supports multiple clients: Windows Store, Windows Phone, iOS, Xamarin iOS, and Xamarin Android. (In the future, we will add support for Android and HTML clients.)

We recently released offline support for Windows Universal Apps, so you can now use a common codebase to create offline-enabled apps that work on both Windows Store and Windows Phone.

When your app is in offline mode, users can still create and modify data, which will be saved to a local store. When the app is back online, it can synchronize local changes with the Mobile Services backend. The feature also includes support for detecting conflicts when the same record is changed on both the client and the backend.

If you’re already using Mobile Services, it’s easy to add offline support to your app. To use, open your Universal solution and click Manage NuGet Packages for solution. Select “Include Prerelease” and search for the package WindowsAzure.MobileServices.SQLiteStore. Versions 1.0.0-alpha4 and above support Universal apps. Install the package into both your Windows Store and Windows Phone projects.

Next, when connecting to your mobile service, use the method GetSyncTable instead of GetTable:

IMobileServiceSyncTable todoTable = App.MobileService.GetSyncTable(); // offline access

Now, set up a local sync store. This code can go in the event handler of OnNavigatedTo, for instance. You can define your own sync store or use the provided SQLite-based implementation:

if (!App.MobileService.SyncContext.IsInitialized)
{
  var store = new MobileServiceSQLiteStore("localsync.db");
  store.DefineTable();
  await App.MobileService.SyncContext.
         InitializeAsync(store, new MobileServiceSyncHandler());
}

Your app should now use IMobileServiceSyncTable (instead of IMobileServiceTable) for CRUD operations. This will save changes to the local database and also keep a log of the changes. When the app is ready to synchronize its changes with the Mobile Service, use the methods PushAsync and PullAsync:

await App.MobileService.SyncContext.PushAsync();
await todoTable.PullAsync();

The code above covered Universal apps, but it’s just as easy to add offline sync support to apps for iOSXamarin iOS, and Xamarin Android.

To learn more about the feature, should check out the following resources:

 

If you have any questions about Azure Mobile Services or the offline feature, please reach out to @lindydonna on Twitter.