I am using Xamarin Forms and Azure in an attempt to implement offline sync in my mobile app to store a list of Machine
objects.
In my class that handles offline sync, I have a function called Initialize()
, for setting up a local database in my phone or tablet, for offline sync
public MobileServiceClient client { get; set; }
IMobileServiceSyncTable<Machine> machineTable;
public async Task Initialize()
{
if (client?.SyncContext?.IsInitialized ?? false)
{
return;
}
var azureUrl = "http://mycoolwebsite.azurewebsites.net";
//Create our client
client = new MobileServiceClient(azureUrl);
//InitialzeDatabase for path
var path = "mylocaldb.db";
path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);
//setup our local sqlite store and intialize our table
var store = new MobileServiceSQLiteStore(path);
//Define table
store.DefineTable<Machine>();
//Initialize SyncContext -- Populate the local DB!
await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler()); //<-- EXCEPTION HERE
machineTable = client.GetSyncTable<Machine>();
// table.PurgeAsync();
}
When I run this code, I run into an exception on the line I have highlighted above. What am I missing? For reference, I am following this tutorial.
It turns out that when using:
await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
machineTable = client.GetSyncTable<Machine>();
I needed to have an public string Id { get; set; }
variable in my Machine
object, as the code for building the local db requires this field. Would be nice if this was added to the documentation somewhere!