这是我的delete函数,它确实找到了workday1对象:

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();
}

当它执行removeFromRealm方法时,它会崩溃:
java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on.它被删除了吗?
我该怎么解决?任何帮助都将不胜感激。
更新(我可以打印以下方法返回的内容):
    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());

最佳答案

您正在尝试删除尚未提交到域的对象。
在这种特殊情况下,如果由于某种原因您不想再提交对象,您只需取消事务即可。

08-06 21:36