339
339

Reputation: 42

How to use an existing Realm as a resource

I would like to put a Realm database which already has some data into Android's assets folder as a read-only resource file, what should I do?

Upvotes: 1

Views: 976

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

As per https://realm.io/docs/java/latest/#read-only-realms

You can put the the_realm.realm file into the /assets folder

Then you can build a configuration (which makes it readOnly() as well) like so

RealmConfiguration config = new RealmConfiguration.Builder()
    .assetFile("the_realm.realm")
    .readOnly()
    // It is optional, but recommended to create a module that describes the classes
    // found in your bundled file. Otherwise if your app contains other classes
    // than those found in the file, it will crash when opening the Realm as the
    // schema cannot be updated in read-only mode.
    .modules(new BundledRealmModule())
    .build();

Then BundledRealmModule would be like

@RealmModule(classes={Dog.class, Cat.class})
public class BundledRealmModule {
}

Upvotes: 4

Related Questions