Using Realm Persistence Library with Android Sync SDK

Contentstack’s Realm Persistence Library for Android SDK helps you save the app data on the device that it is being accessed on. This enables your app to serve data offline, i.e., even when it’s not connected to the internet.

This Persistent Library contains methods that are required to map data fields of your content types and Realm for data storage.

Let’s look at how to use this library for your Contentstack-powered Android apps.

Note: If you have just started with Android SDK and Contenstack, we recommend reading more about Realm and Contentstack docs before proceeding with the following steps.

Download the sync wrapper files from here and unzip it. This folder contains the following four files:

RealmStore.java

SyncManager.java

SyncPersistable.java

SyncStore.java

Add this folder to your project.

Map data

To start mapping of data, first, you need to create a content type schema (in Contentstack) as per your app design and create entries. For your convenience, we have already created the necessary content types. Download them from here and import them to your app’s stack in Contentstack.

Next, create entries for the imported content types. In order to sync this data with Realm, we need to add data mappings. The three important items to be mapped in our Synchronization process are as follows:

Sync token/Pagination token

Entries

Assets

Let’s look at how each of the above can be mapped.

Sync token mapping

To save sync token and pagination token, you need the SyncStore table which will manage the storage and retrieval of updated sync token and pagination token.

Entry mapping

To begin with, let’s consider an example of our Conference app. Let’s say we have two content types: Session and Speaker. And, the ‘Session’ content type has a reference field that refers to multiple entries of the Speaker content type. Let’s see how to implement this example.

Create a table class named Session extending RealmObject, and add following code to implement EntityFields as shown below: