ArcGIS Runtime SDK for Qt

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:

  • Create a GeodatabaseSyncTask, passing in the URL of the feature service to be edited.
  • Set up and connect to a slot to report on the progress while the synchronization happens.
  • Set up the parameters for the synchronization task.
  • Call the syncGeodatabase method on GeodatabaseSyncTask.
For a complete example, see the sample Edit and sync features.

GenerateGeodatabaseParameters EditAndSyncFeatures::getGenerateParameters(Envelope gdbExtent)
{
  // create the parameters
  GenerateGeodatabaseParameters params;
  params.setReturnAttachments(false);
  params.setOutSpatialReference(SpatialReference::webMercator());
  params.setExtent(gdbExtent);
   // set the layer options for 1 layer
  QList<GenerateLayerOption> layerOptions;
  GenerateLayerOption generateLayerOption(m_featureLayerId);
  layerOptions << generateLayerOption;
  params.setLayerOptions(layerOptions);
   return params;
}
SyncGeodatabaseParameters EditAndSyncFeatures::getSyncParameters()
{
  // create the parameters
  SyncGeodatabaseParameters params;
  params.setGeodatabaseSyncDirection(SyncDirection::Bidirectional);
   // set the layer options for 1 layer
  QList<SyncLayerOption> layerOptions;
  SyncLayerOption syncLayerOption(m_featureLayerId);
  layerOptions << syncLayerOption;
  params.setLayerOptions(layerOptions);
   return params;
}
void EditAndSyncFeatures::generateGeodatabaseFromCorners(double xCorner1, double yCorner1, double xCorner2, double yCorner2)
{
  // create an envelope from the QML rectangle corners
  auto corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
  auto corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
  auto extent = Envelope(corner1, corner2);
  auto geodatabaseExtent = GeometryEngine::project(extent, SpatialReference::webMercator());
   // get the updated parameters
  auto params = getGenerateParameters(geodatabaseExtent);
   // execute the task and obtain the job
  QString outputGdb = QQmlProperty::read(this, "outputGdb").toString();
  auto generateJob = m_syncTask->generateGeodatabase(params, outputGdb);
   // connect to the job's status changed signal
  if (generateJob)
  {
    connect(generateJob, &GenerateGeodatabaseJob::jobStatusChanged, this, [this, generateJob]()
    {
      // connect to the job's status changed signal to know once it is done
      switch (generateJob->jobStatus()) {
      case JobStatus::Failed:
        emit updateStatus("Generate failed");
        emit hideWindow(5000, false);
        break;
      case JobStatus::NotStarted:
        emit updateStatus("Job not started");
        break;
      case JobStatus::Paused:
        emit updateStatus("Job paused");
        break;
      case JobStatus::Started:
        emit updateStatus("In progress...");
        break;
      case JobStatus::Succeeded:
        m_isOffline = true;
        m_offlineGdb = generateJob->result();
        emit isOfflineChanged();
        emit updateStatus("Complete");
        emit hideWindow(1500, true);
        emit updateInstruction("Tap on a feature");
        addOfflineData();
        break;
      default:
        break;
      }
    });
     // start the generate job
    generateJob->start();
  }
void EditAndSyncFeatures::executeSync()
{
  // get the updated parameters
  auto params = getSyncParameters();
   // execute the task and obtain the job
  auto syncJob = m_syncTask->syncGeodatabase(params, m_offlineGdb);
   // connect to the job's status changed signal
  if (syncJob)
  {
    connect(syncJob, &GenerateGeodatabaseJob::jobStatusChanged, this, [this, syncJob]()
    {
      // connect to the job's status changed signal to know once it is done
      switch (syncJob->jobStatus()) {
      case JobStatus::Failed:
        emit updateStatus("Sync failed");
        emit hideWindow(5000, false);
        break;
      case JobStatus::NotStarted:
        emit updateStatus("Job not started");
        break;
      case JobStatus::Paused:
        emit updateStatus("Job paused");
        break;
      case JobStatus::Started:
        emit updateStatus("In progress...");
        break;
      case JobStatus::Succeeded:
        m_isOffline = true;
        emit isOfflineChanged();
        emit updateStatus("Complete");
        emit hideWindow(1500, true);
        emit updateInstruction("Tap on a feature");
        break;
      default:
        break;
      }
    });
     // start the sync job
    syncJob->start();
  }

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

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.setGeodatabaseSyncDirection() 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.

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 registerSyncEnabledGeodatabase() 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