Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
This product is retired. For a replacement for projects using .NET 8 or later, see the Community Toolkit Datasync library.
This tutorial covers the offline sync feature of Azure Mobile Apps for the Xamarin.Android quickstart app. Offline sync allows end users to interact with a mobile app—viewing, adding, or modifying data—even when there's no network connection. Changes are stored in a local database. Once the device is back online, these changes are synced with the remote backend.
Before starting this tutorial, you should have completed the Xamarin.Android Quickstart Tutorial, which includes creating a suitable backend service. We also assume you have added authentication to your application. However, this is optional.
In online operation, you read to and write from a IRemoteTable<T>
. When using offline sync, you read to and write from a IOfflineTable<T>
instead. The IOfflineTable
is backed by an on-device SQLite database, and synchronized with the backend database.
In Visual Studio:
Right-click on the TodoApp
solution, then select Manage NuGet Packages for Solution....
In the new tab, select Browse, then enter Microsoft.Datasync.Client in the search box.
Select the Microsoft.Datasync.Client.SQLiteStore
package.
In the right-hand pane, select all the client projects (except the TodoAppService.NET6
project).
Select Install.
Accept the license agreement when prompted.
Open the TodoApp.Data
project and locate the RemoteTodoService.cs
class (within the Services
directory). Update the class as follows:
Add the following using
statement to the top of the file:
using Microsoft.Datasync.Client.SQLiteStore;
Change the definition of _table
to be an IOfflineTable<TodoItem>
:
/// <summary>
/// Reference to the table used for datasync operations.
/// </summary>
private IOfflineTable<TodoItem> _table = null;
Add a new property for storing the offline database location:
/// <summary>
/// The path to the offline database
/// </summary>
public string OfflineDb { get; set; }
Update the InitializeAsync
method to define the offline database:
// Create the offline store definition
var connectionString = new UriBuilder { Scheme = "file", Path = OfflineDb, Query = "?mode=rwc" }.Uri.ToString();
var store = new OfflineSQLiteStore(connectionString);
store.DefineTable<TodoItem>();
var options = new DatasyncClientOptions
{
OfflineStore = store,
HttpPipeline = new HttpMessageHandler[] { new LoggingHandler() }
};
// Create the datasync client.
_client = TokenRequestor == null
? new DatasyncClient(Constants.ServiceUri, options)
: new DatasyncClient(Constants.ServiceUri, new GenericAuthenticationProvider(TokenRequestor), options);
// Initialize the database
await _client.InitializeOfflineStoreAsync();
// Get a reference to the offline table.
_table = _client.GetOfflineTable<TodoItem>();
// Set _initialized to true to prevent duplication of locking.
_initialized = true;
Update the RefreshItemsAsync()
to do offline synchronization:
/// <summary>
/// Refreshes the TodoItems list manually.
/// </summary>
/// <returns>A task that completes when the refresh is done.</returns>
public async Task RefreshItemsAsync()
{
await InitializeAsync();
// First, push all the items in the table.
await _table.PushItemsAsync();
// Then, pull all the items in the table.
await _table.PullItemsAsync();
return;
}
In the TodoApp.Android
project, edit the App.xaml.cs
file. Change the definition of the RemoteTodoService
as follows:
TodoService = new RemoteTodoService(async () => await GetAuthenticationToken())
{
OfflineDb = Xamarin.Essentials.FileSystem.AppDataDirectory + "/offline.db"
};
If you haven't completed the authentication tutorial, the definition should look like this instead:
TodoService = new RemoteTodoService()
{
OfflineDb = Xamarin.Essentials.FileSystem.AppDataDirectory + "/offline.db"
};
The app doesn't synchronize with the backend until the refresh icon is pressed. To test:
Open the Azure portal.
Open the resource group that contains the resources for the quickstart.
Select the quickstart
database.
Select the Query editor (preview).
Log in with SQL server authentication using the same credentials you set up for the database.
In the query editor, enter SELECT * FROM [dbo].[TodoItems]
. Then select Run.
A list of the current TodoItems will be displayed.
Now, make some changes through your app. DO NOT PRESS REFRESH (yet).
Repeat the SQL statement in the Azure portal and verify that no changes have been made to the data in the database.
Select the Refresh icon on your app to push the data in queue to the backend service. You'll see the HTTP transactions happening in the Output Debug window.
Repeat the SQL statement in the Azure portal and verify that your changes have been pushed to the remote service.
Unless you're doing another quick start tutorial, you can delete the resources associated with the backend service now.
You can also use the Azure CLI:
az group delete -g quickstart
If you used the Azure Developer CLI to deploy resources, you can use the azd down
command instead.
The deletion will take a few minutes to complete.
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in