Page updated Jan 16, 2024

Conflict resolution

If data synchronization is enabled via AppSync, there can be different versions of the same object on the client and server. Multiple clients may have updated their respective copies of an object. DataStore will converge different object versions by applying conflict detection and resolution strategies. The default resolution is called Auto Merge. This strategy allows collections to grow, and prefers server-side versions of single-field data. Other strategies include Optimistic Concurrency control and Custom Lambda functions. For more information, see the AWS AppSync documentation on conflict handling.

Custom conflict resolution

To select a different conflict resolution strategy, navigate into your project from a terminal and run amplify update api. Choose Conflict resolution strategy to change the conflict detection and resolution strategies.

1? Please select from one of the below mentioned services:
2 `GraphQL`
3...
4? Select a setting to edit
5 `Conflict resolution strategy`
6? Select the default resolution strategy
7 Auto Merge
8❯ Optimistic Concurrency
9 Custom Lambda
10 Learn More

Per model configuration

Note that this flow will also allow you to change the strategy on each individual GraphQL type, though it is recommended to use the same strategy for your whole schema unless you have an advanced use case:

1? Do you want to override default per model settings? Yes
2? Select the models from below:
3❯◉ Post
4PostEditor
5User
6
7? Select the resolution strategy for Post model Custom Lambda
8? Select from the options below (Use arrow keys)
9Create a new Lambda Function
10 Existing Lambda Function

Custom configuration

DataStore has a few optional configurations, such as the ability to specify a custom handler for error messages that take place in any part of the system. You can also specify a custom conflict handler that runs if a mutation is rejected by AWS AppSync during one of the conflict resolution strategies.

Finally you can configure the number of records to sync as an upper bound on items (per-Model) which will be stored locally on the device, as well as a custom interval in minutes which is an override of the default 24 hour "base query" which runs as part of the Delta Sync process.

Custom configuration fields

  • errorHandler - handler function when DataStore encounters an unrecoverable error in one of its ongoing background operations (model synchronization).
  • conflictHandler - handler function when there is a conflict between two local and remote model instances in a sync operation.
  • syncMaxRecords - sets the maximum number of records, from the server, to process from a sync operation.
  • syncPageSize - sets the number of items requested in each page of sync results.
  • syncInterval - sets the duration of time after which delta syncs will not be preferred over base syncs. The default time unit is minutes.
  • syncExpression - sets a sync expression for a particular model to filter which data is synced locally. The expression is evaluated each time DataStore is started. The QueryPredicate is applied on both sync and subscriptions.

Example

The code below illustrates a conflict resolution handler for the Post model that retries a mutation with the same title, but the most recent remote data for all other fields. The conflict resolution handler discards conflicts for all other models (by passing ConflictResolutionDecision.applyRemote() to onDecision.accept(...)).

1DataStoreConfiguration config = DataStoreConfiguration.builder()
2 .errorHandler(error -> {
3 // handle DataStore exception here
4 Log.e("YourApp", "Error.", error);
5 })
6 .conflictHandler((conflictData, onDecision) -> {
7 // retry mutation with the same title and the most recent remote data for other fields
8 if (conflictData.getRemote() instanceof Post) {
9 Post patched = ((Post) conflictData.getRemote())
10 .copyOfBuilder()
11 .title(((Post) conflictData.getLocal()).getTitle())
12 .build();
13 onDecision.accept(ConflictResolutionDecision.retry(patched));
14 } else {
15 onDecision.accept(ConflictResolutionDecision.applyRemote());
16 }
17 })
18 // Set the duration of time after which delta syncs will not be preferred over base syncs
19 .syncInterval(1, TimeUnit.DAYS)
20 // Set the maximum number of records, from the server, to process from a sync operation
21 .syncMaxRecords(10_000)
22 // Set the number of items requested in each page of sync results
23 .syncPageSize(1_000)
24 .build();
25AWSDataStorePlugin dataStorePlugin = AWSDataStorePlugin.builder().dataStoreConfiguration(config).build();
1val config = DataStoreConfiguration.builder()
2 .errorHandler {
3 // handle DataStore exception here
4 Log.e("YourApp", "Error.", it)
5 }
6 .conflictHandler { conflictData, onDecision ->
7 // retry mutation with the same title and the most recent remote data for other fields
8 if (conflictData.remote is Post) {
9 val patched = (conflictData.remote as Post)
10 .copyOfBuilder()
11 .title((conflictData.local as Post).title)
12 .build()
13 onDecision.accept(ConflictResolutionDecision.retry(patched))
14 } else {
15 onDecision.accept(ConflictResolutionDecision.applyRemote())
16 }
17 }
18 // Set the duration of time after which delta syncs will not be preferred over base syncs
19 .syncInterval(1, TimeUnit.DAYS)
20 // Set the maximum number of records, from the server, to process from a sync operation
21 .syncMaxRecords(10_000)
22 // Set the number of items requested in each page of sync results
23 .syncPageSize(1_000)
24 .build()
25val dataStorePlugin = AWSDataStorePlugin.builder().dataStoreConfiguration(config).build()