我有下课
public class Student extends RealmObject{
private int studentID;
private String studentName;
// getters and setters here
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将值设置为已创建的学生对象
student.setStudentName("Peter");
Run Code Online (Sandbox Code Playgroud)
然后我得到以下错误
java.lang.IllegalStateException:读取事务期间的可变方法调用.
为了克服这个问题,我必须按照以下方式做到这一点
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
student.setStudentName("Peter");
realm.commitTransaction();
Run Code Online (Sandbox Code Playgroud)
我不想在数据库中保留此更改.如何在不将数据库持久保存到数据库的情况下,将值设置/更改为域对象变量?
当您使用 时Realm.createObject()
,对象将被添加到 Realm 中,并且它仅在写入事务中工作。您可以取消交易并从而丢弃该对象。
此外,您可以将模型类用作独立类并在内存中创建对象(有关详细信息,请参阅http://realm.io/docs/java/0.80.0/#creating-objects )。如果需要持久化对象,可以使用该Realm.copyToRealm()
方法。
归档时间: |
|
查看次数: |
6426 次 |
最近记录: |