How to create a Realm database with source data for my Android application?

I am trying to create a database for my Android application using Realm. I need the data to be pre-populated when the application is installed. Realm migration configuration as part of RealmConfiguration is not performed when the database version is 0 (default is 0). How to add data at the first launch of the application?

+6
source share
3 answers

Realm Java 0.89 introduced a method that allows you to specify a transaction when creating a Realm database for the first time. This method, RealmConfiguration.Builder.initialData(Realm.Transaction transaction) , is called part of the RealmConfiguration Builder configuration.

for instance

 RealmConfiguration config = new RealmConfiguration.Builder(context) .name("myrealm.realm") .initialData(new MyInitialDataRealmTransaction()), .build(); 
+6
source

What I'm doing right now works to check if this is installed the first time my application is installed and creates a new object.

 if (Preferences.freshInstall(getApplicationContext())) { Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); Category inbox = new Category("Inbox", "#445566"); realm.copyToRealm(inbox); realm.commitTransaction(); Preferences.notNew(getApplicationContext()); } 

There must be a better way to do this with Realm Migrations

0
source

Initial configuration of data transactions, as @Benjamin shows in Realm Java, works! I just want him to attend Realm Cocoa.

I created a problem for this in the Github tracker here, # 3877 .

0
source

All Articles