Search code examples
androidrealm

Using dynamic realm in android


Is there anyone used dynamic realm in android.I'm searching a lot in net but I cannot find any sample code using dynamic realm database, except this realm documentation in android

RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
    DynamicRealm realm1 = DynamicRealm.getInstance(realmConfig);

 // In a DynamicRealm all objects are DynamicRealmObjects
    DynamicRealmObject person = realm1.createObject("Person");

// All fields are accessed using strings
    String name = person.getString("name");
    int age = person.getInt("age");

// An underlying schema still exists, so accessing a field that does not exist
// will throw an exception
    person.getString("I don't exist");

// Queries still work normally
    RealmResults<DynamicRealmObject> persons = realm1.where("Person")
            .equalTo("name", "John")
            .findAll();

but when I run this code I'm getting this error message.

  The class class_Person doesn't exist in this Realm.     

can anyone help me about this and show me a code sample?


Solution

  • A DynamicRealm does not automatically generate any schemas, so if you have a Person model class, you cannot access it dynamically before you opened a Realm normally using Realm.getInstance() (as that will create the schema). Otherwise you manually have to create the Person class using DynamicRealm.getSchema()