Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm with pre populated data into assets?

Tags:

android

realm

Normally I use Realm as:

RealmConfiguration config = new RealmConfiguration.Builder(applicationContext).deleteRealmIfMigrationNeeded().build();

How can I add to the assets folder of my project a database with data and read it?

like image 691
georgeok Avatar asked Nov 12 '15 15:11

georgeok


People also ask

What is a data Realm?

A Realm is an instance of a Realm Mobile Database container. Realms can be local, synchronized, or in-memory. In practice, your application works with any kind of Realm the same way. In-memory Realms have no persistence mechanism, and are meant for temporary storage.

What type of database is Realm?

Realm is an open source object database management system, initially for mobile operating systems (Android/iOS) but also available for platforms such as Xamarin, React Native, and others, including desktop applications (Windows), and is licensed under the Apache License.

When would you use a Realm database?

The Realm database is the object-based database that will make it easier for you, especially in such complex development projects. Realm helps developers in gracefully handling the unpredictable environment of mobile apps in which devices can shut down at any time; connections can be lost, etc.

Is Realm a noSQL database?

Realm is essentially a noSQL database which means that with Realm, you can store and retrieve data that is modeled in means other than tabular relations of relational databases. This makes for a much more familiar data structure that is easier to maintain.


2 Answers

Since Realm Java 0.91.0 there has been an assetFile(String) option on the RealmConfiguration that automatically will copy a file from assets and use that if needed (e.g. if the Realm is opened the first time or has been deleted for some reason):

RealmConfiguration config = new RealmConfiguration.Builder()
  .assetFile("path/to/file/in/assets") // e.g "default.realm" or "lib/data.realm"
  .deleteRealmIfMigrationNeeded()
  .build()

The above will copy the file from assets the first time the Realm is opened or if it has been deleted due to migrations (remember to update the asset Realm in that case).


OLD ANSWER:

It is possible to bundle a Realm database in the assets folder, but then you just need to copy it from there when starting the app the first time.

We have an example of how to copy the files here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/MigrationExampleActivity.java#L101-Lundefined

copyBundledRealmFile(this.getResources().openRawResource(R.raw.default_realm), "default.realm");

private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
    try {
        File file = new File(this.getFilesDir(), outFileName);
        FileOutputStream outputStream = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, bytesRead);
        }
        outputStream.close();
        return file.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
like image 136
Christian Melchior Avatar answered Sep 21 '22 20:09

Christian Melchior


Since Realm 0.89.0 RealmConfiguration.initialData(Realm.Transaction) can now be used to populate a Realm file before it is used for the first time.

RealmConfiguration conf = new RealmConfiguration.Builder(context)
.initialData(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                  realm.beginTransaction();
                  realm.createObject(....)
                  realm.commitTransaction();
                }
            }).deleteRealmIfMigrationNeeded().name("mRealm.db").build();
Realm realm = Realm.getInstance(conf);
like image 31
georgeok Avatar answered Sep 17 '22 20:09

georgeok