max
max

Reputation: 6187

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?

Upvotes: 4

Views: 3066

Answers (2)

kirankumar
kirankumar

Reputation: 11

in order to create new instance of the existing schema in the realmDB 
you have to create an instance by calling realm method createObject inside realm method beginTransaction and commitTransaction like mentioned below.


realm.beginTransaction();
DynamicRealmObject person = realm.createObject("Person");
person.setString("name", "kiran");
person.setInt("age", 29);
realm.commitTransaction();

and the methods setString and setInt are the methods of the DynamicRealmObject. 
Please go through the documentation https://realm.io/docs/java/latest/api/io/realm/DynamicRealm.html for more understanding of the DynamicRealms.

Upvotes: 1

Christian Melchior
Christian Melchior

Reputation: 20126

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()

Upvotes: 1

Related Questions