Was this article helpful?
Thanks for your feedback
The Sync API takes care of syncing your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates. Contentstack’s Android SDK supports Sync API, which you can use to build powerful apps.
This guide will help you understand how to use the Sync API with Contentstack Android SDK.
The Initial sync request performs a complete sync of your app data. It returns all the published entries and assets of the specified stack in response.
To start the Initial sync process, use the sync method.
stack.sync(new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack,Error error) { if (error == null) { // Success block } else { // Error block } } });
Note: Use the value of the delivery token against the access_token key.
The response also contains a sync token, which you need to store, since this token is used to get subsequent delta updates later, as shown in the Subsequent sync section below.
You can also fetch custom results in initial sync by using advanced sync queries.
If the result of the initial sync (or subsequent sync) contains more than 100 records, the response would be paginated. It provides pagination token in the response. However, you don’t have to use the pagination token manually to get the next batch; the SDK does that automatically until the sync is complete.
You can use pagination token in case you want to fetch only selected batches. It is especially useful if the sync process is interrupted midway (due to network issues, etc.). In such cases, this token can be used to restart the sync process from where it was interrupted.
Note: If any changes are made to the entries (if any actions i.e., Unpublish/Publish/Delete have been performed), then you need to run the init call again and generate a new pagination_token to fetch the remaining records.
stack.syncPaginationToken("pagination_token", new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack, Error error) { if (error == null) { // Success block } else { // Error block } } });
You can use the sync token (that you receive after initial sync) to get the updated content next time. The sync token fetches only the content that was added after your last sync, and the details of the content that was deleted or updated.
Note: If you have made any changes to the entries (performed any actions i.e., Unpublish/Publish/Delete), then you need to run the init call again and generate a new pagination_token to fetch the remaining records.
stack.syncToken("sync_token", new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack,Error error) { if (error == null) { // Success block } else { // Error block } } });
Remember that you get a sync token in response after every sync. So, don’t forget to store it every time.
You can use advanced sync queries to fetch filtered results. Let's look at them in detail.
For initializing sync from a specific date, you can use syncFromDate and specify the date as follows:
stack.syncFromDate(date, new SyncResultCallBack() { @Override public void onCompletion(SyncResult syncResult,Error error) { if (error == null) { // Success block } else { // Error block } } });
You can also initialize sync with entries of only specific content types. To do this, use syncContentType and specify the content type UID as its value.
However, if you do this, the subsequent syncs will only include the entries of the specified content types.
stack.syncContentType(Stack.Types.type, new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack,Error error) { if (error == null) { // Success block } else { // Error block } } });
You can also initialize sync with entries of only specific locales. To do this, use syncLocale and specify the locale code as its value.
However, if you do this, the subsequent syncs will only include the entries of the specified locales.
stackInstance.syncLocale(Language, new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack,Error error) { if (error == null) { // Success block } else { // Error block } } });
You can also initialize sync with entries and assets based on a specific publish type. The acceptable values are entry_published, entry_unpublished, entry_deleted, asset_published, asset_unpublished, asset_deleted, and content_type_deleted. To do this, use syncPublishType and specify the parameters.
However, if you do this, the subsequent syncs will only include the entries of the specified publish type
stack.syncPublishType(Stack.PublishType.entry_published, new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack, Error error) { if (error == null) { // Success block } else { // Error block } } });
You can also initialize sync with entries that satisfy multiple parameters. To do this, use sync and specify the parameters.
However, if you do this, the subsequent syncs will only include the entries of the specified parameters
stackInstance.sync(date,"content_type", Stack.Types.type, Language, new SyncResultCallBack() { @Override public void onCompletion(SyncStack syncStack,Error error) { if (error == null) { // Success block } else { // Error block } } });
Was this article helpful?
Thanks for your feedback