Using the Xamarin.Android preview of Mobile Services offline
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
andSystem.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
fromIMobileServiceTable<>
toIMobileServiceSyncTable<>
In the method
OnCreate(Bundle)
, after the line that initializes the memberclient
, 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 methodGetSyncTable<>
instead ofGetTable<>
:toDoTable = client.GetSyncTable <ToDoItem> ();
Modify the method
OnRefreshItemsSelected
to add calls toPushAsync
andPullAsync
: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.
Comments
- Anonymous
July 16, 2014
I am getting an error when I put "install-package WindowsAzure.MobileServices.SQLiteStore -Pre" into the consoleInstall-Package : Could not install package 'Microsoft.Bcl.Build 1.0.14'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v4.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. - Anonymous
October 28, 2014
The comment has been removed