Search code examples
androidrealm

Multiple tables in Android Realm


Is it possible to have multiple tables within the same realm in Android?

If it is possible (as some other SO answers indicate, like this one: How to truncate all tables in realm android), how would queries work?

Thanks!


Solution

  • Is it possible to have multiple tables within the same realm in Android?

    Yes, you just have to define multiple RealmObjects.

    public class Foo extends RealmObject {
        // fields
    }
    
    public class Bar extends RealmObject {
        // fields
    }
    

    Queries work by "table". You can query for one table at a time, and you can also query across object links (relationships) if you really want.

    RealmResults<User> result2 = realm.where(User.class)
                                  .equalTo("name", "John")
                                  .or()
                                  .equalTo("name", "Peter")
                                  .findAllSorted("name", Sort.ASCENDING);