Multiple Realm databases in one Android app

1,667 views
Skip to first unread message

Valeria Rogatchevskikh

unread,
Jun 8, 2015, 5:07:43 AM6/8/15
to realm...@googlegroups.com
Hi,

According to your documentation it says it is possible to have multiple realms. it says in the documentation:

Other Realms

It’s sometimes useful to have multiple realms, persisted at different locations, for example if you have different data groupings, different databases per feature, or you need to package some read-only files with your app, separate from the database your users will be editing.


Unfortunately this is not true, at least it does not work for me. I have added 2 databases to raw resources and both databases have one different class in them: 

database 1: classA

database 2: classB

I the copy these databases to a writable folder but when I want to open database1 it says that classB is missing and if I open database 2 it says classA is missing. So what is the point in having 2 databases if you still have to have all the tables used in the app in each one of them?

I checked the Migration project here:

https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java

but it does not apply to my case since I have not changed the tables at all I just want 2 separate databases with 2 separate classes in each one of them. 

Would there be anyway I could keep my 2 separate databases (I don't need to write in them, only read)?


I know I could just have both tables  in one file but before doing that I wanted to make sure there was no other way.


Here are the steps I use for each one of the databases:

1. copy database from raw folder to context.getFilesDir()/databaseYYY.realm

2. get database with this.realm2 = Realm.getInstance(context, "databaseYYY.realm");

On the getInstance line I get the following exception:

 io.realm.exceptions.RealmMigrationNeededException: The ClassA class is missing from the schema for this Realm.


After reading the source code I realized that you are looking for all the classes that extend RealmObject so there is no way the statement written above can work, unless I am missing something.

For now I only see 2 possibilities: having only one database with both classes or insert missing table in each one of the databases.

Any suggestions?

Thanks in advance,

Christian Melchior

unread,
Jun 8, 2015, 5:17:21 AM6/8/15
to Valeria Rogatchevskikh, realm...@googlegroups.com
Hi Valeria

You are right that up until now all Realms were required to know all classes extending RealmObject. However we are just in the process of merging a new feature called RealmModules that allow you to do exactly what you request, ie. choosing which classes go to which Realm.

We have not published any documentation on this outside GitHub as this hasn't been properly released yet, but you can try it out by using our SNAPSHOT build: https://github.com/realm/realm-java#using-snapshots

You can see an example on how this works in this example: https://github.com/realm/realm-java/tree/cm-realm-module-example/examples/realmModuleAppExample/src/main/java/io/realm/examples/appmodules


--
Christian Melchior
Senior Android Developer


--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/169e99c4-5a6a-4dba-9695-356a5f10ca6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:96307294-1272#}

Valeria Rogatchevskikh

unread,
Jun 8, 2015, 8:39:49 AM6/8/15
to Christian Melchior, realm...@googlegroups.com
Hi Christian,

Thank you very much for such a quick reply. Although the example you provides seems to fulfill my requirements unfortunately I was not able to try it. 
I tried adding the snapshot dependency, the latest one in Frog which is v8.1 at the time of this writing. This version does not have the RealmConfiguration class yet. I also tried to build the project from source but without success. I will open another thread for that since it is completely different problem.

Thanks,

Valeria Rogatchvevskikh


Christian Melchior

unread,
Jun 9, 2015, 3:01:33 AM6/9/15
to Valeria Rogatchevskikh, realm...@googlegroups.com
Hi Valeria

Yes, apparently we had some problems with our deployment pipeline. We are working on fixing it right now. I hope you managed to build from source using NDK r10d until we can get this fixed.


--
Christian Melchior
Senior Android Developer


On Mon, Jun 8, 2015 at 12:40 PM UTC, Valeria Rogatchevskikh <val...@letgo.com> wrote:
Hi Christian,

Thank you very much for such a quick reply. Although the example you
provides seems to fulfill my requirements unfortunately I was not able to
try it.
I tried adding the snapshot dependency, the latest one in Frog
{#HS:96307294-1272#}

Valeria Rogatchevskikh

unread,
Jun 9, 2015, 3:24:05 AM6/9/15
to Christian Melchior, realm...@googlegroups.com
Hi Christian,

Thank you I was able to compile the library but I have another issue now which is not really related so I posted in stackoverflow. 
If you create a realm in one Android app, you should be able to use it in another Android application without a problem, right?

Thanks,

Valeria

Christian Melchior

unread,
Jun 9, 2015, 3:28:33 AM6/9/15
to Valeria Rogatchevskikh, realm...@googlegroups.com
Yes, copying the database should work also across Android/iOS. We will take a look at your Stack Overflow question.


--
Christian Melchior
Senior Android Developer


{#HS:96307294-1272#}

Christian Melchior

unread,
Jun 9, 2015, 4:05:18 AM6/9/15
to Valeria Rogatchevskikh, realm...@googlegroups.com
Hi Valeria

Just to let you know that our SNAPSHOT's are being deployed again. You can find the latest using 0.81.0-SNAPSHOT


--
Christian Melchior
Senior Android Developer


On Tue, Jun 9, 2015 at 7:28 AM UTC, Help <he...@realm.io> wrote:
Yes, copying the database should work also across Android/iOS. We will take a look at your Stack Overflow question.

--
Christian Melchior
Senior Android Developer


{#HS:96307294-1272#}
Reply all
Reply to author
Forward
0 new messages