Azure – SQL Offline Support

  1. Add NuGet Package WindowsAzure.MobileService.SQLiteStore
    Search for „WindowsAzure.MobileService“
  2. Change Reference from „SQLite for … (Windows 8.1)“ to „SQLite for Universal App Platform“
  3. Insert usings for
    • Microsoft.WindowsAzure.MobileServices.SQLiteStore
    • Microsoft.WindowsAzure.MobileServices.Sync
  4. Change the „GetTable“-Function to „GetSyncTable“-Function
  5. Add „await InitLocalStoreAsync()“-Function in „OnNavigatedTo“-Function
  6. Add „await SyncAsync()“-Function after „InsertAsync“-Function
  7. Add „await SyncAsync()“-Function after „UpdateAsync“-Function
  8. Add „await SyncAsync()“-Function after „DeleteAsync“-Function
  9. Add „await SyncAsync()“-Function before „Refresh“-Function
  10. Add following Code:
private async Task RefreshItemsAsync()
{
  MobileServiceInvalidOperationException exception = null;
  try
  {
    items = await todoTable.where(...).ToCollectionAsync();
  }
  catch(MobileServiceInvalidOperationException e)
  {
    exception = e;
  }

  if(exception !=  null)
  {
    await new MessageDialog(...).ShowAsync();
  }
  else
  {
    ListItems.ItemsSource = items;
    this.ButtonSave.IsEnabled = true;
  }
}

private async Task InitLocalStoreAsync()
{
  if(!App.MobileService.SyncContext.IsInitialized)
  {
    var store = new MobileServiceSQLiteStore("localstore.db");
    store.DefineTable<TodoItem>();
    await App.MobileService.SyncContext.InitializeAsync(store);
  }

  await SyncAsync();
}

private async Task SyncAsync()
{
  string errorString = null;

  try
  {
    await App.MobileService.SyncContext.PushAsync();
    await todoTable.PullAsync("todoItems", todoTable.CreateQuery());    
  }
  catch(MobileServicePushFailedException ex)
  {
    errorString = "Errors: " + ex.PushResult.Errors.Count + " " + ex.Message;
  }
  catch(Exception ex)
  {
    errorString = "Error: " + ex.Message;
  }

  if(errorString != null)
  {
    MessagebDialog d = new MessageDialog(errorString);
    await d.ShowAsync();
  }
}