The following article covers a presentation and demo given at Xamarin Dev Days in Mauritius. The article demonstrates how to get started with Azure mobile apps, and benefit the powers of the cloud in terms of Scalability, Offline Sync, and Data Analytics.
- Getting Started With Xamarin And Azure - Part One
- Getting Started With Xamarin And Azure - Extend The Sample App - Part Two
Introduction
The objective of this article is to build a sample app discussed in part 2 and add the Offline Sync capabilities to it. People using mobile apps are often on the move and are not always connected. Despite not having connectivity, the application users need to be able to continue working and retrieving their data in offline mode.
To achieve this, the Azure Mobile Service SDK provides offline sync capabilities, using SQL Lite as local storage.
Implementation
To add offline sync, both the front-end and the back-end, codes of the client need to be modified. Follow the steps, given below, to proceed with the changes.
- Install the SQL Lite
Install the NuGet package Microsoft.Azure.Mobile.Client.SQLiteStore in both, the portable class and all the clients that will consume the service.
- Modify the Feedback Manager class
- Use IMobileServiceSyncTable instead of IMobileServiceTable
This provides operations on local table. Therefore, all the operations will be done against the Sync table.
- Define a MobileServicesSQLLiteStore in the constructor
Add the following code to the constructor. This will create a new local store and define a table to use this store.- var store = new MobileServiceSQLiteStore("localstore5.db");
- store.DefineTable<Feedback>();
- //Initializes the SyncContext using the default IMobileServiceSyncHandler.
- this.client.SyncContext.InitializeAsync(store);
- Add the function SyncAsync
This function will update the server with all the information in the client side and will also update the client if there are any changes.- public async Task SyncAsync()
- {
- try
- {
- await this.client.SyncContext.PushAsync();
- await this.feedbackTable.PullAsync(
- //The first parameter is a query name that is used internally by the client SDK to implement incremental sync.
- //Use a different query name for each unique query in your program
- "allFeedbacks",
- this.feedbackTable.CreateQuery());
- }
- catch (MobileServicePushFailedException exc)
- {
- if (exc.PushResult != null)
- {
- }
- }
- }
- Modify the function GetFeedbacksAsync
Add the following code to the function, GetFeedbacksAsync, so that the local database is refreshed before sending the list of feedback to the client.- if (syncItems)
- {
- await this.SyncAsync();
- }
- Front-end Code change
On the front-end, we add a button to Sync the database.
<Button Text="Sync" MinimumHeightRequest="30" Clicked="OnSync" />
This will call the method OnSync with SyncItems = true which will, then, call the GetFeedbacksAsync that refreshes the local database before displaying the feedback.- public async void OnSync(object sender, EventArgs e)
- {
- await RefreshItems(true, true);
- }






Join the conversation! Your thoughts help the community grow.