Android realm.io: Row/Object is no longer valid

荒凉一梦 提交于 2020-01-14 19:11:27

问题


This is my delete function and it does find the workday1 object:

public static void delete(Context context, Workday workday) {
    Realm realm = getRealm(context);
    realm.beginTransaction();

    Workday workday1 = realm.where(Workday.class)
            .equalTo("date", workday.getDate())
            .equalTo("hours", workday.getHours())
            .equalTo("minutes", workday.getMinutes())
            .findFirst();

    workday1.removeFromRealm();
    realm.commitTransaction();
}

When it executes the removeFromRealm method it crashes:

java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?

How can I fix this? Any help would be greatly appreciated.

UPDATE (I can print the content returned by the following method):

    Workday workday1 = realm.where(Workday.class)
            .equalTo("date", workday.getDate())
            .equalTo("hours", workday.getHours())
            .equalTo("minutes", workday.getMinutes())
            .findFirst();

    System.out.println("--------------------------------");
    System.out.println(workday1.getHours());


回答1:


You are trying to remove an object you have not committed to the Realm yet.

In this particular case, if for some reason you don't want to commit the object anymore, you can simply cancel the transaction.




回答2:


In my case, the problem was that the same Object I was deleting was in an Adapter. After I made the Adapter extend RealmBaseAdapter the problem stopped.



来源:https://stackoverflow.com/questions/27331518/android-realm-io-row-object-is-no-longer-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!