realm数据库报错:Changing Realm data can only be done from inside a transaction.

Posted catherineliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了realm数据库报错:Changing Realm data can only be done from inside a transaction.相关的知识,希望对你有一定的参考价值。

在编写realm数据库相关时:

代码:

  List<Student> delByStudent(String priNum){
        RealmResults<Student> studentsList = myStudentRealm.where(Student.class).equalTo("num",priNum).findAll();
        for (int i = 0;i<studentsList.size();i++)
            studentsList.deleteFirstFromRealm();
        return studentsList;

在for的循环语句

studentsList.deleteFirstFromRealm();

报错:????

java.lang.IllegalStateException: Changing Realm data can only be done from inside a transaction.
原因:要成功更改数据库realm的数据需要在事务里。

解决方法:

myStudentRealm.beginTransaction();
//  change data to realm
myStudentRealm.commitTransaction();
故,修改后的代码如下:??????
 //  delete student by primary key(StuNum)
    List<Student> delByStudent(String priNum){
        RealmResults<Student> studentsList = myStudentRealm.where(Student.class).equalTo("num",priNum).findAll();
        myStudentRealm.beginTransaction();
        for (int i = 0;i<studentsList.size();i++) {
            studentsList.deleteLastFromRealm();
            Log.e("stu","-------delet_by-------"+studentsList);
        }
        myStudentRealm.commitTransaction();
        return studentsList;

以上是关于realm数据库报错:Changing Realm data can only be done from inside a transaction.的主要内容,如果未能解决你的问题,请参考以下文章

ORA-27101: shared memory realm does not exist

windows kerberos客户端执行kinit报错:kinit: Cannot find KDC for realm

realm清空所有数据库的数据

iOS开发-Realm数据库

shiro入门 之 Realm

shiro自定义realm,模拟数据库查询验证