ArcGIS Runtime SDK for .NET

Sync offline edits

Your users can edit offline and sync their edits to a feature service later when connectivity is restored. Syncing offline edits requires that a local geodatabase is created from an existing sync-enabled feature service either from ArcGIS Online or ArcGIS Enterprise. A user can take a single sync-enabled feature service offline or a map containing one or more sync-enabled feature services offline.

If you take a single feature service offline and edit that single geodatabase, you can sync that local data with the feature service using the GeodatabaseSyncTask. Alternatively, if you take a whole map offline and create a geodatabase for each of the sync-enabled feature services in the map, you can sync all of the geodatabases in the map with one call using the OfflineMapSyncTask.

You should note that 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.

Synchronize a single geodatabase

To synchronize edits made to a single geodatabase, 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.

// 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();
}

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.

Synchronize a map

You can synchronize all the edits made to all the sync-enabled geodatabases in your map using the offline map sync task.

  • Create an OfflineMapSyncTask.
  • Create an OfflineMapSyncParameters object with appropriate values.
  • Create an OfflineMapSyncJob by calling SyncOfflineMap on the task, passing in the parameters object you defined.
  • Handle the OfflineMapSyncJob.JobChanged event to track status changes and messages for the job. This is where job completion (success or failure) will be reported.
  • Optionally handle the OfflineMapSyncJob.ProgressChanged event to monitor and report the job's progress.
  • Call OfflineMapSyncJob.Start to start the sync job.
// Create a new offline map sync task
OfflineMapSyncTask offlineMapSyncTask = await OfflineMapSyncTask.CreateAsync(map);


// Create the offline map sync parameters
OfflineMapSyncParameters parameters = new OfflineMapSyncParameters
{
    RollbackOnFailure = true,
    SyncDirection = SyncDirection.Bidirectional
};


// Instantiate the sync job using the synchronization parameters
OfflineMapSyncJob offlineMapSyncJob = offlineMapSyncTask.SyncOfflineMap(parameters);


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


// Start the job
offlineMapSyncJob.Start();

Register a geodatabase workflow

To work offline with a sync-enabled feature service, you need to download a geodatabase containing the features to your device. As mentioned in the Create an offline layer and Create an offline map topics, you can download a geodatabase to your device using any of these methods:

  • GenerateGeodatabase on GeodatabaseSyncTask
  • GenerateOfflineMap on OfflineMapTask
  • DownloadPreplannedOfflineMap on OfflineMapTask
Each of these methods will automatically register the geodatabase with the feature service so that you can sync any changes with the feature service as described above.

If, however, you transfer that geodatabase file to another device, for example by side loading the file, the feature service will not be able to perform a sync with that geodatabase. If you wish to enable sync you must register the geodatabase with the feature service you used to generate the original geodatabase. To do this you must use the use RegisterGeodatabaseAsync method on GeodatabaseSyncTask to register each geodatabase copy (on each device). 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.

Related topics