当前位置:Gxlcms > 数据库问题 > realm数据库报错:Changing Realm data can only be done from inside a transaction.

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

时间:2021-07-01 10:21:17 帮助过:22人阅读

在编写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.

标签:find   state   解决方法   elb   ast   pre   ima   mys   list   

人气教程排行