Store huge data in to the Realm database


I am working on a content based app and Realm has been perfectly fine for me. The only problem is I have no idea where to store data in the first place. I have about 500 strings that should be in to the realm file the moment user opens the app. I can use an Application extended class and write the code inside the onCreate method, but isn’t there a better way to store mass data other than writing the same line for about 500 times?
P. S: I use one transaction, but even in this case it does not seem standard and clean code at all knowing that I already have all that data in the realm file and don’t want to run that code all over again.


Edit your question with some code, this way we can see what can be improved! :slight_smile:


Then why dont you just simply ship your initial realm with the app?


My code looks like this:

myRealm = Realm.getDefaultInstance();
myRealm.executeTransactionAsync(new Realm.Transaction(){
	public void execute(Realm realm){
		//TheContent extends from RealmObject and has an int field for "id" and a string field for "value"
		TheContent myContent = new TheContent();
		//record 1
		myContent.setValue("this is the value i want to insert in the first record");
		//record 2
		myContent.setValue("this is the value i want to insert in the second record");
		//record 3
		myContent.setValue("this is the value i want to insert in the third record");

But I am pretty sure that is not the correct way to handle what I intend.


I really would like to do that. But as far as I know there is not a way to create realm databases outside the code. I even checked out Realm Studio but it seems it can only edit/manipulate existing databases.


I am a little bit busy now and can’t think too much about the problem, but a possible solution would be:

List<TheContent> theContents  = Arrays.asList(new Gson().fromJson(contentsJson, TheContens[].class))

Where contentsJson should be a json string representing an array of all your contents.
Then you could use this List with inititalData() method like this:

RealmConfiguration config = new RealmConfiguration.Builder()
                .initialData(new Realm.Transaction() {
                    public void execute(Realm realm) {


This way when your database file is created for the first time (and only for the first time), your database will be prepopulated with this data.

FYI: The second code block should be on your Application class!


Yeah, thats not the good way, but it is definitely working.

The solution is simple: insert all of your data in a development app, copy the realm out and use that as the initial realm.

With realm studio, you can import data, see here: