Syncing data to cloud
Once you're happy with your application, you can start syncing with the cloud by provisioning a backend from your project. DataStore can connect to remote backend and automatically sync all locally saved data using GraphQL as a data protocol.
Setup cloud sync
Synchronization between offline and online data can be tricky. DataStore's goal is to remove that burden from the application code and handle all data consistency and reconciliation between local and remote behind the scenes, while developers focus on their application logic. Up to this point the focus was to setup a local data store that works offline and has all the capabilities you would expect from a data persistence framework.
The next step is to make sure the locally saved data is synchronized with a cloud backend powered by AWS AppSync.
Add the API plugin
Although DataStore presents a distinct API, its cloud synchronization functionality relies on the underlying API category. Therefore, you will still be required to incorporate the API plugin when working with DataStore.
Make sure that you declare a dependency on the API plugin in your app-level build.gradle
:
dependencies { // Add this line. implementation 'com.amplifyframework:aws-api:ANDROID_V1_VERSION'}
Next, add the plugin in your Amplify initialization code alongside with the previously added AWSDataStorePlugin
.
Amplify.addPlugin(new AWSDataStorePlugin());// Add this line.Amplify.addPlugin(new AWSApiPlugin());Amplify.configure(getApplicationContext());
Amplify.addPlugin(AWSDataStorePlugin())// Add this line.Amplify.addPlugin(AWSApiPlugin())Amplify.configure(applicationContext)
Push the backend to the cloud
By now you should have a backend created with conflict detection enabled, as described in the Getting started guide.
Check the status of the backend to verify if it is already provisioned in the cloud.
amplify status
You should see a table similar to this one.
| Category | Resource name | Operation | Provider plugin || -------- | ----------------- | --------- | ----------------- || Api | amplifyDatasource | No Change | awscloudformation |
In case Operation
says Create
or Update
you need to push the backend to the cloud.
amplify push
Existing backend
DataStore can connect to an existing AWS AppSync backend that has been deployed from another project, no matter the platform it was originally created in. In these workflows it is best to work with the CLI directly by running an amplify pull
command from your terminal and then generating models afterwards, using the process described in the Getting started guide.
For more information on this workflow please see the Multiple Frontends documentation.
Distributed data
When working with distributed data, it is important to be mindful about the state of the local and the remote systems. DataStore tries to make that as simple as possible for you; however, some scenarios might require some consideration.
For instance, when updating or deleting data, one has to consider that the state of the local data might be out-of-sync with the backend. This scenario can affect how conditions should be implemented.
Update and delete with predicate
For such scenarios both the save()
and the delete()
APIs support an optional predicate which will be sent to the backend and executed against the remote state.
Amplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]"), update -> Log.i("MyAmplifyApp", "Post updated successfully!"), failure -> Log.e("MyAmplifyApp", "Could not update post, maybe the title has been changed?", failure));
Amplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]"), { Log.i("MyAmplifyApp", "Post updated successfully!") }, { Log.e("MyAmplifyApp", "Could not update post, maybe the title has been changed?", it) })
try { Amplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]")) Log.i("MyAmplifyApp", "Post updated successfully!") } catch (error: DataStoreException) { Log.e("MyAmplifyApp", "Could not update post, maybe the title has been changed?", error) }
RxAmplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]")) .subscribe( update -> Log.i("MyAmplifyApp", "Post updated successfully!"), failure -> Log.e("MyAmplifyApp", "Could not update post, maybe the title has been changed?", failure) );
There's a difference between the traditional local condition check using if/else
constructs and the predicate in the save()
and delete()
APIs as you can see in the example below.
// Tests only against the local stateif (post.getTitle().startsWith("[Amplify]")) { Amplify.DataStore.save(post, update -> { /* handle result */ }, failure -> { /* handle failure */} );}
// Only applies the update if the data in the remote backend satisfies the criteriaAmplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]"), update -> { /* handle result */ }, failure -> { /* handle failure */ });
// Tests only against the local stateif (post.title.startsWith("[Amplify]")) { Amplify.DataStore.save(post, { /* handle result */ }, { /* handle failure */} )}
// Only applies the update if the data in the remote backend satisfies the criteriaAmplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]"), { /* handle result */ }, { /* handle failure */ })
// Tests only against the local stateif (post.title.starts("[Amplify]")) { try { Amplify.DataStore.save(post) } catch (error: DataStoreException) { // handle error }}
// Only applies the update if the data in the remote backend satisfies the criteriaAmplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]"), { /* handle result */ }, { /* handle failure */ })
// Tests only against the local stateif (post.getTitle().startsWith("[Amplify]")) { RxAmplify.DataStore.save(post) .subscribe( update -> { /* handle result */ }, failure -> { /* handle failure */} );}
// Only applies the update if the data in the remote backend satisfies the criteriaRxAmplify.DataStore.save(post, Post.TITLE.beginsWith("[Amplify]")) .subscribe( update -> { /* handle result */ }, failure -> { /* handle failure */ } );
Conflict detection and resolution
When concurrently updating the data in multiple places, it is likely that some conflict might happen. For most of the cases the default Auto-merge algorithm should be able to resolve conflicts. However, there are scenarios where the algorithm won't be able to be resolved, and in these cases, a more advanced option is available and will be described in detail in the conflict resolution section.
Clear local data
Amplify.DataStore.clear()
provides a way for you to clear all local data if needed. This is a destructive operation but the remote data will remain intact. When the next sync happens, data will be pulled into the local storage again and reconstruct the local data.
One common use for clear()
is to manage different users sharing the same device or even as a development-time utility.
// Listen for sign out events.final String signedOutEventName = AuthChannelEventName.SIGNED_OUT.toString();
Amplify.Hub.subscribe(HubChannel.AUTH, anyAuthEvent -> signedOutEventName.equals(anyAuthEvent.getName()), // When one arrives, clear the DataStore. signedOutEvent -> Amplify.DataStore.clear( () -> Log.i("MyAmplifyApp", "DataStore is cleared."), failure -> Log.e("MyAmplifyApp", "Failed to clear DataStore.") ));
Amplify.Hub.subscribe(HubChannel.AUTH, { // Listen for sign out events. it.name.equals(AuthChannelEventName.SIGNED_OUT.toString()) }, { // When one arrives, clear the DataStore. Amplify.DataStore.clear( { Log.i("MyAmplifyApp", "DataStore is cleared") }, { Log.e("MyAmplifyApp", "Failed to clear DataStore") } ) })
Amplify.Hub.subscribe(HubChannel.AUTH) { it.name == AuthChannelEventName.SIGNED_OUT.toString() } // When sign out event arrives, clear the DataStore. .onEach { Amplify.DataStore.clear() } .catch { Log.e("MyAmplifyApp", "Failed to clear DataStore.", it) } .collect { Log.i("MyAmplifyApp", "DataStore is cleared.") }
// Listen for sign out events.final String signedOutEventName = AuthChannelEventName.SIGNED_OUT.toString();
RxAmplify.Hub.on(HubChannel.AUTH) .filter(event -> signedOutEventName.equals(event.getName())) .flatMapObservable(RxAmplify.DataStore::clear) .subscribe( () -> Log.i("MyAmplifyApp", "DataStore is cleared."), failure -> Log.e("MyAmplifyApp", "Failed to clear DataStore.") );
This is a simple yet effective example. However, in a real scenario you might want to only call clear()
when a different user is signedIn
in order to avoid clearing the database for a repeated sign-in of the same user.
Selectively syncing a subset of your data
By default, DataStore fetches all the records that you’re authorized to access from your cloud data source to your local device. The maximum number of records that will be stored locally is configurable here.
You can utilize selective sync to persist a subset of your data instead.
Selective sync works by applying predicates to the base and delta sync queries, as well as to incoming subscriptions.
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> Post.RATING.gt(5)) .syncExpression(Comment.class, () -> Comment.STATUS.eq("active")) .build()) .build());
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post::class.java) { Post.RATING.gt(5) } .syncExpression(Comment::class.java) { Comment.STATUS.eq("active") } .build()) .build())
RxAmplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> Post.RATING.gt(5)) .syncExpression(Comment.class, () -> Comment.STATUS.eq("active")) .build()) .build());
When DataStore starts syncing, only Posts with rating > 5
and Comments with status
equal to active
will be synced down to the user's local store.
Reevaluate expressions at runtime
Sync expressions get evaluated whenever DataStore starts. In order to have your expressions reevaluated, you can execute Amplify.DataStore.clear()
or Amplify.DataStore.stop()
followed by Amplify.DataStore.start()
.
If you have the following expression and you want to change the filter that gets applied at runtime, you can do the following:
public Integer rating = 5;
public void initialize() { Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> Post.RATING.gt(rating)) .build()) .build());}
public void changeSync() { rating = 1; Amplify.DataStore.stop( () -> Amplify.DataStore.start( () -> Log.i("MyAmplifyApp", "DataStore started"), error -> Log.e("MyAmplifyApp", "Error starting DataStore: ", error) ), error -> Log.e("MyAmplifyApp", "Error stopping DataStore: ", error) );}
var rating: Int = 5;
fun initialize() { Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post::class.java) { Post.RATING.gt(rating) } .build()) .build())}
fun changeSync() { rating = 1; Amplify.DataStore.stop( { Amplify.DataStore.start( { Log.i("MyAmplifyApp", "DataStore started") }, { Log.e("MyAmplifyApp", "Error starting DataStore", it) } ) }, { Log.e("MyAmplifyApp", "Error stopping DataStore", it) } )}
var rating = 5
fun initialize() { Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post::class.java) { Post.RATING.gt(rating) } .build()) .build())}
suspend fun changeSync() { rating = 1 try { Amplify.DataStore.stop() Amplify.DataStore.start() Log.i("MyAmplifyApp", "DataStore started") } catch (error: DataStoreException) { Log.w("MyAmplifyApp", "Failed to restart DataStore", error) }}
public Integer rating = 5;
public void initialize() { RxAmplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> Post.RATING.gt(rating)) .build()) .build());}
public void changeSync() { rating = 1; RxAmplify.DataStore.stop() .andThen(RxAmplify.DataStore.start()) .subscribe( () -> Log.i("MyAmplifyApp", "DataStore restarted"), error -> Log.e("MyAmplifyApp", "Error restarting DataStore: ", error) );}
Each time DataStore starts (via start
or any other operation: query
, save
, delete
, or observe
), DataStore will reevaluate the syncExpressions
.
In the above case, the predicate will contain the value 1
, so all Posts with rating > 1
will get synced down.
Keep in mind: Amplify.DataStore.stop()
will retain the local store's existing content. Run Amplify.DataStore.clear()
to clear the locally-stored contents.
public void changeSync() { rating = 8; Amplify.DataStore.clear( () -> Amplify.DataStore.start( () -> Log.i("MyAmplifyApp", "DataStore started"), error -> Log.e("MyAmplifyApp", "Error starting DataStore: ", error) ), error -> Log.e("MyAmplifyApp", "Error clearing DataStore: ", error) );}
fun changeSync() { rating = 8; Amplify.DataStore.clear( { Amplify.DataStore.start( { Log.i("MyAmplifyApp", "DataStore started") }, { Log.e("MyAmplifyApp", "Error starting DataStore", it) } ) }, { Log.e("MyAmplifyApp", "Error clearing DataStore", it) } )}
suspend fun changeSync() { rating = 8 try { Amplify.DataStore.clear() Amplify.DataStore.start() Log.i("MyAmplifyApp", "DataStore started") } catch (error: DataStoreException) { Log.w("MyAmplifyApp", "Error clearing/starting DataStore", error) }}
public void changeSync() { rating = 8; RxAmplify.DataStore.clear() .andThen(RxAmplify.DataStore.start() .subscribe( () -> Log.i("MyAmplifyApp", "DataStore cleared and restarted"), error -> Log.e("MyAmplifyApp", "Error clearing or restarting DataStore: ", error) );}
This will clear the contents of your local store, reevaluate your sync expressions and re-sync the data from the cloud, applying all of the specified predicates to the sync queries.
You can also have your sync expression return QueryPredicates.all()
in order to remove any filtering for that model. This will have the same effect as the default sync behavior.
public Integer rating = null;
public void initialize() { Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> { if (rating != null) { return () -> Post.RATING.gt(rating); } return QueryPredicates.all(); }) .build()) .build());}
var rating = null
fun initialize() { Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post::class.java) { if (rating != null) { Post.RATING.gt(rating) } QueryPredicates.all() } .build()) .build())}
public Integer rating = null;
public void initialize() { RxAmplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(Post.class, () -> { if (rating != null) { return () -> Post.RATING.gt(rating); } return QueryPredicates.all(); }) .build()) .build());}
Advanced use case - Query instead of Scan
You can configure selective sync to retrieve items from DynamoDB with a query operation against a GSI. By default, the base sync will perform a scan. Query operations enable a highly efficient and cost-effective data retrieval for customers running DynamoDB at scale. Learn about creating GSIs with the @index
directive here.
In order to do that, your syncExpression
should return a predicate that maps to a query expression.
For example, for the following schema:
type User @model { id: ID! firstName: String! lastName: String! @index(name: "byLastName", sortKeyFields: ["createdAt"]) createdAt: AWSDateTime!}
To construct a query expression, return a predicate with the primary key of the GSI. You can only use the eq
operator with this predicate.
For the schema defined above User.LAST_NAME.eq("Doe")
is a valid query expression.
Optionally, you can also chain the sort key to this expression, using any of the following operators: eq | ne | le | lt | ge | gt | beginsWith | between
.
E.g., User.LAST_NAME.eq("Doe").and(User.CREATED_AT.gt("2020-10-10")
.
Both of these sync expressions will result in AWS AppSync retrieving records from Amazon DynamoDB via a query operation:
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User.class, () -> User.LAST_NAME.eq("Doe")) .build()) .build());
// OR
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User.class, () -> User.LAST_NAME.eq("Doe").and(User.CREATED_AT.gt("2020-10-10"))) .build()) .build());
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User::class.java) { User.LAST_NAME.eq("Doe") } .build()) .build())
// OR
Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User::class.java) { User.LAST_NAME.eq("Doe").and(User.CREATED_AT.gt("2020-10-10")) } .build()) .build())
RxAmplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User.class, () -> User.LAST_NAME.eq("Doe")) .build()) .build());
// OR
RxAmplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration( DataStoreConfiguration.builder() .syncExpression(User.class, () -> User.LAST_NAME.eq("Doe").and(User.CREATED_AT.gt("2020-10-10"))) .build()) .build());