createObject

inline fun <T : RealmModel> Realm.createObject(): T

Instantiates and adds a new object to the Realm.

This method is only available for model classes with no @PrimaryKey annotation. If you like to create an object that has a primary key, use createObject instead.

Return

the new object.

Parameters

T

the Class of the object to create.

Throws

io.realm.exceptions.RealmException

if the primary key is defined in the model class or an object cannot be created.

inline fun <T : RealmModel> Realm.createObject(primaryKeyValue: Any?): T

Instantiates and adds a new object to the Realm with the primary key value already set.

If the value violates the primary key constraint, no object will be added and a RealmException will be thrown. The default value for primary key provided by the model class will be ignored.

Return

the new object.

Parameters

T

the Class of the object to create.

primaryKeyValue

value for the primary key field.

Throws

io.realm.exceptions.RealmException

if object could not be created due to the primary key being invalid.

if the model class does not have an primary key defined.

if the primaryKeyValue doesn't have a value that can be converted to the expected value.