Page updated Nov 10, 2023

Observe in real time

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.") );
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.")

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 and 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(; 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());
7Consumer<Cancelable> observationStarted = value ->{
8 Log.d(tag, "success on cancelable");
10Consumer<DataStoreException> onObservationError = value ->{
11 Log.d(tag, "error on snapshot$value");
13Action onObservationComplete = () ->{
14 Log.d(tag, "complete");
16QueryPredicate predicate =
17 Post.TITLE.beginsWith("post").and(;
18QuerySortBy querySortBy = new QuerySortBy("post", "rating", QuerySortOrder.ASCENDING);
19List<QuerySortBy> sortByList = new ArrayList<QuerySortBy>();
21ObserveQueryOptions options = new ObserveQueryOptions(predicate, sortByList);
23 Post.class,
24 options,
25 observationStarted,
26 onQuerySnapshot,
27 onObservationError,
28 onObservationComplete