Page updated Nov 9, 2023

Observe in real time

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Observe model mutations in real-time

You can subscribe to changes on your Models. This reacts dynamically to updates of data to the underlying Storage Engine, which could be the result of GraphQL Subscriptions as well as Queries or Mutations that run against the backing AppSync API if you are synchronizing with the cloud.

Amplify.DataStore.observe(Post.class, cancelable -> Log.i("MyAmplifyApp", "Observation began."), postChanged -> { Post post = postChanged.item(); Log.i("MyAmplifyApp", "Post: " + post); }, failure -> Log.e("MyAmplifyApp", "Observation failed.", failure), () -> Log.i("MyAmplifyApp", "Observation complete.") );
1Amplify.DataStore.observe(Post.class,
2 cancelable -> Log.i("MyAmplifyApp", "Observation began."),
3 postChanged -> {
4 Post post = postChanged.item();
5 Log.i("MyAmplifyApp", "Post: " + post);
6 },
7 failure -> Log.e("MyAmplifyApp", "Observation failed.", failure),
8 () -> Log.i("MyAmplifyApp", "Observation complete.")
9);

Observe query results in real-time

observeQuery(...) returns an initial data set, similar to query(...), and also automatically subscribes to subsequent changes to the query.

The first snapshot returned from observeQuery will contain the same results as calling query directly on your Model - a collection of all the locally-available records. Subsequent snapshots will be emitted as updates to the dataset are received in the local store.

While data is syncing from the cloud, snapshots will contain all of the items synced so far and an isSynced status of false. When the sync process is complete, a snapshot will be emitted with all the records in the local store and an isSynced status of true.

In addition to typical real-time use cases, observeQuery can be used on app launch to show your customers an initial data set from the local store while new data is being synced from cloud.

observeQuery also accepts the same predicates and sorting options as query.

String tag = "ObserveQuery"; Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value ->{ Log.d(tag, "success on snapshot"); Log.d(tag, "number of records: " + value.getItems().size()); Log.d(tag, "sync status: " + value.getIsSynced()); }; Consumer<Cancelable> observationStarted = value ->{ Log.d(tag, "success on cancelable"); }; Consumer<DataStoreException> onObservationError = value ->{ Log.d(tag, "error on snapshot$value"); }; Action onObservationComplete = () ->{ Log.d(tag, "complete"); }; QueryPredicate predicate = Post.TITLE.beginsWith("post").and(Post.RATING.gt(10)); QuerySortBy querySortBy = new QuerySortBy("post", "rating", QuerySortOrder.ASCENDING); List<QuerySortBy> sortByList = new ArrayList<QuerySortBy>(); sortByList.add(querySortBy); ObserveQueryOptions options = new ObserveQueryOptions(predicate, sortByList); Amplify.DataStore.<Post>observeQuery( Post.class, options, observationStarted, onQuerySnapshot, onObservationError, onObservationComplete );
1String tag = "ObserveQuery";
2Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value ->{
3 Log.d(tag, "success on snapshot");
4 Log.d(tag, "number of records: " + value.getItems().size());
5 Log.d(tag, "sync status: " + value.getIsSynced());
6};
7Consumer<Cancelable> observationStarted = value ->{
8 Log.d(tag, "success on cancelable");
9};
10Consumer<DataStoreException> onObservationError = value ->{
11 Log.d(tag, "error on snapshot$value");
12};
13Action onObservationComplete = () ->{
14 Log.d(tag, "complete");
15};
16QueryPredicate predicate =
17 Post.TITLE.beginsWith("post").and(Post.RATING.gt(10));
18QuerySortBy querySortBy = new QuerySortBy("post", "rating", QuerySortOrder.ASCENDING);
19List<QuerySortBy> sortByList = new ArrayList<QuerySortBy>();
20sortByList.add(querySortBy);
21ObserveQueryOptions options = new ObserveQueryOptions(predicate, sortByList);
22Amplify.DataStore.<Post>observeQuery(
23 Post.class,
24 options,
25 observationStarted,
26 onQuerySnapshot,
27 onObservationError,
28 onObservationComplete
29);