Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Realm database with initial data for my android app?

Tags:

android

realm

I am trying to create a database for my android application using Realm. I need to have data that is pre-populated when the app is installed. Setting a Realm Migration as part of the RealmConfiguration does not run when the version of the database is 0 (defaults to 0 initially). How can I add data the first time the application is setup?

like image 358
Bill Anderson-Blough Avatar asked Feb 09 '16 23:02

Bill Anderson-Blough


1 Answers

Realm Java 0.89 introduced a method that allows for specifying a transaction to be run when a Realm database is created for the first time. This method, RealmConfiguration.Builder.initialData(Realm.Transaction transaction), is called as part of setting up the RealmConfiguration Builder.

For example

RealmConfiguration config = new RealmConfiguration.Builder(context)
  .name("myrealm.realm")
  .initialData(new MyInitialDataRealmTransaction()), 
  .build();
like image 62
Jade Avatar answered Nov 08 '22 20:11

Jade