Sync offline edits

Your users can edit offline using a services pattern and later sync their edits back to a feature service when connected. Syncing offline edits requires that you've created a geodatabase using a sync-enabled feature service from ArcGIS Online or ArcGIS Enterprise, as described in Create an offline map. After users have made edits and are ready to sync their local copy of the data with the service, use GeodatabaseSyncTask to sync with the feature service. Syncing can be performed even if no edits have been made locally, to pull changes from the feature service into the local copy of the data.

Note:

A Basic license is required for editing.

To synchronize edits, do the following:

  • Define synchronization parameters, such as the direction in which edits should be synchronized.
  • Create a new GeodatabaseSyncTask that points to the URL of the feature service.
  • Call SyncGeodatabase on the task to create a new SyncGeodatabaseJob. Pass in the synchronization parameters and the local geodatabase.
  • Handle the JobChanged event to report status and messages as the job runs.
  • Await the asynchronous completion of the job.

Note:
The sync operation overwrites previously synced edits to the same features on the service.

Internally, ArcGIS Runtime uses a geodatabase transaction to wrap the synchronization process. Since only one current transaction is permitted on a geodatabase, you will receive an error if you attempt to synchronize while another transaction is active (in an editing workflow, for example). Similarly, you may get an error if you try to start a transaction while synchronization is in progress. Errors that arise during a sync operation are returned in the callback when the job is done. For descriptions of errors that can arise when syncing offline edits, see Error handling with sync.

For services backed by non-versioned data, sync operations are performed per-layer, and are always bi-directional—that is, local edits are uploaded to the service, and then new edits are downloaded from the service. For services backed by versioned data, sync operations are per-geodatabase, and you can change the synchronization parameters to determine in which direction edits are synchronized—download only, upload only, or bi-directional. Use Geodatabase.SyncModel to find out if a geodatabase can be synchronized per-layer or per-geodatabase. Use SyncGeodatabaseParameters.GeodatabaseSyncDirection to set the synchronization direction for a sync operation. When using bi-directional sync, note that the 'last in wins'—that is, uploaded edits will overwrite changes present on the server.

Register a geodatabase in a pre-planned workflow

In a services pattern workflow sometimes known as a pre-planned workflow, you generate the geodatabase once and load copies of it onto each user's device. If you've generated the geodatabase on the user's device with , you don't need to register a geodatabase.

In the pre-planned workflow, you use the RegisterSyncEnabledGeodatabaseAsync method to register each geodatabase copy (on each device) with the feature service you used to generate the original geodatabase. Registering in this way ensures each device receives the correct updates during sync operations.

Caution:

  • Registering a geodatabase with a feature service is not supported with a versioned feature service.
  • Once you call unregister on a geodatabase, you cannot re-register the same geodatabase.
  • If the original geodatabase is ever unregistered, no additional clients can use that copy to register.

For a list of benefits of this workflow, see create an offline layer.

Example code

The following example shows you how to sync your offline edits back to a feature service.

// function to submit a geodatabase synchronization job 
// the URL for the feature service and the path to the local geodatabase are passed in
public async Task SyncronizeEditsAsync(string serviceUrl, string geodatabasePath)
{
    // create sync parameters
    var taskParameters = new SyncGeodatabaseParameters()
    {
        RollbackOnFailure = true,
        GeodatabaseSyncDirection = SyncDirection.Bidirectional
    };


    // create a sync task with the URL of the feature service to sync
    var syncTask = await GeodatabaseSyncTask.CreateAsync(new Uri(serviceUrl));


    // open the local geodatabase
    var gdb = await Esri.ArcGISRuntime.Data.Geodatabase.OpenAsync(geodatabasePath);


    // create a synchronize geodatabase job, pass in the parameters and the geodatabase
    SyncGeodatabaseJob job = syncTask.SyncGeodatabase(taskParameters, gdb);


    // handle the JobChanged event for the job
    job.JobChanged += (s, e) =>
    {
        // report changes in the job status
        if (job.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Succeeded)
        {
            // report success ...
            statusMessage = "Synchronization is complete!";
        }
        else if (job.Status == Esri.ArcGISRuntime.Tasks.JobStatus.Failed)
        {
            // report failure ...
            statusMessage = job.Error.Message;
        }
        else
        {
            statusMessage = "Sync in progress ...";
        }
    };


    // await the completion of the job
    var result = await job.GetResultAsync();
}

Related topics