Was this article helpful?
Thanks for your feedback
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.
To sync data from Contentstack to Realm we need to first set up Realm and then download the wrapper folder. Let's go through the installation processes in detail.
To set up Realm in your application, perform the following steps:
dependencies { classpath "io.realm:realm-gradle-plugin:5.4.0" }For the latest gradle file, refer to the Realm document.
apply plugin: 'realm-android'
@PrimaryKey() @RealmField(name=’uid’) private String uid;
As you are now done setting up the Realm SDK, let's look into the Contentstack SDK setup.
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 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:
Let’s look at how each of the above can be mapped.
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.
if (stackResponse.getPaginationToken()!=null){ persistsToken(stackStack.getPaginationToken()); }else{ persistsToken(stackStack.getSyncToken()); }
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:
// @RealmClass accepts ("name= "content_type_uid"") @RealmClass(name = "session") public class Session extends RealmObject { ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * // Mandatory fields @PrimaryKey @RealmField(name = "uid") private String uid; ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * //Note: the annotation name will be content_type's field id @RealmField(name = "title") //user defined fields may be anything of user liking. private String mTitle; @RealmField(name = "is_popular") private String mIsPopular; @RealmField(name = "type") private String mType; @RealmField(name = "session_id") private String mId; @RealmField(name = "tags") private String mTags; @RealmField(name = "locale") private String mLocale; @RealmField(name = "speakers") private RealmList < Speaker > speaker; @RealmField(name = "track") private String mTrack; @RealmField(name = "start_time") private String mStartTime; @RealmField(name = "end_time") private String mEndTime; @RealmField(name = "room") private Room mRoom; ...Generated getters and setters... }
You also need to implement the fieldMapping function which returns the mapping of attributes and entry fields in Contentstack.
Similarly, we can add other entity and mapping for each entity.
To map Assets, create a table for assets named SysAssets and extend RealmObject. Add the following code to implement AssetProtocol.
@RealmClass(name = "sys_assets") public class SysAssets extends RealmObject { ** ** ** ** ** ** ** ** ** ** ** ** ** ** // Mandatory fields @PrimaryKey @RealmField(name = "uid") private String uid; ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** // Note: the annotation name will be content_type's field id @RealmField(name = "created_at") //user defined fields may be anything of user liking. private String created_at; @RealmField(name = "updated_at") private String updated_at; @RealmField(name = "created_by") private String created_by; @RealmField(name = "updated_by") private String updated_by; @RealmField(name = "content_type") private String content_type; @RealmField(name = "file_size") private String file_size; @RealmField(name = "tags") private String tags; @RealmField(name = "filename") private String filename; @RealmField(name = "url") private String url; @RealmField(name = "is_dir") private String is_dir; @RealmField(name = "parent_uid") private String parent_uid; @RealmField(name = "_version") private String version; @RealmField(name = "title") private String title; @RealmField(name = "publish_details") private String publish_details; ...Generated getters and setters... }
Now, our final step is to initiate SyncManager and begin with the Sync process.
Finally, after setting up the content mapping, initiate SyncManager. It takes Stack instance and Helper class instance as follows:
//Get stack instance as follows Stack stack = Contentstack.stack(context, "api_key", "access_token", "environment"); //Get realm instance as follows Realm realmInstance = Realm.getDefaultInstance(); //Get realmStore instance by passing realmInstance to its constructor as follows RealmStore realmStore = new RealmStore(realmInstance); SyncManager syncManager = new SyncManager(realmStore, stack); syncManager.stackRequest();
Was this article helpful?
Thanks for your feedback