Before we start

The steps below are for the Xamarin plugin for Visual Studio. If you're using Xamarin Studio instead, you should install the nuget addin for Xamarin and most of the instructions should be the same.

Using offline in Xamarin.Android

  • Create a new Mobile Service or use an existing one and download the Xamarin quickstart for Android.
  • Open the project and remove the reference to to Azure Mobile Services SDK in Components (we'll be adding a newer on via NuGet)
  • Install the prerelease package of the Mobile Services SQLiteStore using the following command in the Package Manager Console:

    install-package WindowsAzure.MobileServices.SQLiteStore -Pre

  • In the references node, remove the references to System.IO, System.Runtime and System.Threading.Tasks

Edit ToDoActivity.cs

  • Add the declarations

    using Microsoft.WindowsAzure.MobileServices.Sync;
    using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
    using System.IO;
    
  • Change the type of the member ToDoActivity.toDoTable from IMobileServiceTable<> to IMobileServiceSyncTable<>

  • In the method OnCreate(Bundle), after the line that initializes the member client, add the following code:

    // existing initializer
    client = new MobileServiceClient (applicationURL, applicationKey, progressHandler);
    
    // new code to initialize the SQLite store
    string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test1.db");
    
    if (!File.Exists(path))
    {
        File.Create(path).Dispose();
    }
    
    var store = new MobileServiceSQLiteStore(path);
    store.DefineTable<ToDoItem>();
    
    await client.SyncContext.InitializeAsync(store, new TodoSyncHandler(this));
    
  • In the same method, change the line that initializes toDoTable to use the method GetSyncTable<> instead of GetTable<>:

    toDoTable = client.GetSyncTable <ToDoItem> ();
    
  • Modify the method OnRefreshItemsSelected to add calls to PushAsync and PullAsync:

    async void OnRefreshItemsSelected ()
    {
        await client.SyncContext.PushAsync();
        await toDoTable.PullAsync();
        await RefreshItemsFromTableAsync();
    }
    

Edit ToDoItem.cs

  • Add the using statement:

    using Microsoft.WindowsAzure.MobileServices; 
    
  • Add the following members to the class ToDoItem:

    [Version]
    public string Version { get; set; }
    
    
    public override string ToString()
    {
        return "Text: " + Text + "\nComplete: " + Complete + "\n";
    }
    

Add a new class TodoSyncHandler

Running the app

When you launch the app, the list of items will be empty, since it will no longer read items from the mobile service, but rather from the local store. The action for the Refresh button will call PushAsync and PullAsync, which will push any pending changes, then read items from the mobile service into the local SQLite store.

To manually trigger a conflict, you should synchronize changes, then change the item on the server (through either a REST client or SQL Server Management Studio). Then, change the item locally and perform the refresh gesture. If you added the conflict handler, then you will see a dialog that will ask how to resolve the conflict, in favor of the server or client.