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 AGSGeodatabaseSyncTask 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:

  • Create an AGSGeodatabaseSyncTask.
  • Create or generate sync parameters for the synchronization task.
  • Create an AGSJob by calling syncJobWithParameters on the task, passing in the parameters you created.
  • Call startWithStatusHandler to start the sync operation, defining a block to report the job completion.

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 AGSGeodatabase.syncModel to find out if a geodatabase can be synchronized per-layer or per-geodatabase. Use AGSSyncGeodatabaseParamters.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 AGSGeodatabaseSyncTask, you don't need to register a geodatabase.

In the pre-planned workflow, you use the registerSyncEnabledGeodatabase 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.

//Create the synchronization parameters and set the layer options
 
AGSSyncGeodatabaseParameters *params = [[AGSSyncGeodatabaseParameters alloc] init];
params.layerOptions = syncLayerOptions;
    
//Instantiate a job to submit the sync job on the server using the
//synchronization parameters and the geodatabase containing the edits

self.syncJob = [self.geodatabaseSyncTask syncJobWithParameters:params geodatabase:self.generatedGeodatabase];
    
//Start the sync job by submitting it to server. 
//The statusHandler block is invoked periodically whenever the job's status changes
//The completion block is invked with the result when the job succeeds, or an error if it fails

[self.syncJob startWithStatusHandler:^(AGSJobStatus status) {
 //print the status} 
 completion:^(id  _Nullable result, NSError * _Nullable error) {
  if (error != nil) {
   NSLog(@"Error = %@", error.localizedDescription);
  }
  else {
   NSLog(@"Synchronization complete");
  }
 }
];