Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value to a realm object

Tags:

android

realm

I have following class

public class Student extends RealmObject{
private int studentID;
private String studentName;

// getters and setters here

}

Then I try to set a value to a already created student object

student.setStudentName("Peter");

Then I get following error

java.lang.IllegalStateException: Mutable method call during read transaction.

In order to overcome this I have to do it as follows

Realm realm = Realm.getInstance(this);
realm.beginTransaction();
student.setStudentName("Peter");
realm.commitTransaction();

I don't want to persist this change in the database. How can I just set/change a value to an realm object variable without always persisting it to the database?

like image 940
ChaturaM Avatar asked Mar 23 '15 23:03

ChaturaM


People also ask

How do we create a realm object?

To define a Realm object in your application, create a subclass of RealmObject or implement RealmModel. All Realm objects must provide an empty constructor. All Realm objects must use the public visibility modifier in Java or the open visibility modifier in Kotlin.

What is a realm object?

For features specific to the Professional and/or Enterprise Editions, consult the PE/EE Documentation. The Realm Object Server synchronizes Realms between devices, provides authentication and access control services for Realms, and offers “serverless” event processing through Realm Functions.


1 Answers

If you want to modify the object in a non-persisted manner, you need an unmanaged copy of it.

You can create a copy using realm.copyFromRealm(RealmObject realmObject); method.

like image 85
EpicPandaForce Avatar answered Oct 10 '22 04:10

EpicPandaForce