• 3 min read

Announcing General Availability of Mobile Offline Sync Managed SDK

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. Also, sometimes mobile data plans can be very expensive, so users appreciate apps that limit their network calls. However, many apps don’t work offline, because of the challenges of implementing correct sync behavior.

Fortunately, Azure Mobile Services allows you to easily provide a native sync experience across your iOS, Android, and Windows apps. The feature is available on both the JavaScript and .NET backends, and supports multiple clients: Windows Universal, iOS, Xamarin iOS, Xamarin Android, and Android.

We are pleased to announce general availability of the offline managed client SDK, which is supported on Windows and Xamarin. GA for the iOS and Android SDKs is coming soon.

Offline sync has a number of benefits:

  • Improve app responsiveness by caching server data locally on the device
  • Create robust apps that remain useful when there are network issues
  • Allow end-users to create and modify data even when there is no network access, supporting scenarios with little or no connectivity
  • Sync data across multiple devices and detect conflicts when the same record is modified by two devices
  • Limit network use for customers who don’t have an unlimited data plan

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. Conflicts can then be handled either on the server or the client.

We’ve updated the quickstarts in the Azure portal to make it even easier to add offline support to your app. The quickstarts for Xamarin.iOS and Xamarin.Android are now offline enabled. Because Windows requires that the SQLite VSIX be installed, the Windows quickstart has all the offline code, but commented out.

If you’re already using Mobile Services, it’s easy to make your app offline-enabled. Just add the NuGet package WindowsAzure.MobileServices.SQLiteStore. 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");
    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("queryID", myQuery);

The PullAsync method takes as the first parameter a query ID, which is a string that uniquely identifies the query in your app. The query ID is used for incremental sync, and is used to store the last updated timestamp from the results of the last pull operation.

If you want to opt out of incremental sync, pass null as the query ID. In this case, all records will be retrieved on every call to PullAsync, which can be inefficient.

You can also pass a query to PullAsync, so that you store only a subset of records on the device. However, if your query has a parameter, then the same parameter value has to be part of the query ID.

For more information, see the Windows and Xamarin offline tutorials.

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

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