deleteAllFromRealm(RealmList:此方法仅在托管模式下可用

时间:2018-11-26 13:45:43

标签: android realm

Android Studio 3.2。 ,领域4.1.0

我需要从Realm中删除列表中的所有项目。

我的代码段:

    public static RealmList<Merchant> getMerchantsRealmList() {
        Realm realm = Realm.getDefaultInstance();
        try {
            RealmResults<Merchant> realmResults = realm.where(Merchant.class).findAll();
            RealmList<Merchant> realmList = new RealmList<>();
            realmList.addAll(realmResults.subList(0, realmResults.size()));
            return realmList;
        } finally {
            realm.close();
        }
    }

 public static void updateMerchantList(final List<Merchant> thatMerchantsList) {
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    RealmList<Merchant> localMerchantRealmList = getMerchantsRealmList();
                    if (!EqualsUtil.areEqualContentLists(localMerchantRealmList, thatMerchantsList)) {
                        List<Merchant> itemNotExistInThatMerchants = new ArrayList<>(localMerchantRealmList);
                        itemNotExistInThatMerchants.removeAll(thatMerchantsList);
                        if (itemNotExistInThatMerchants.size() > 0) {
                            localMerchantRealmList.removeAll(itemNotExistInThatMerchants);
                            localMerchantRealmList.deleteAllFromRealm(); // error here
                        }
                    }

                }
            });
        } finally {
            realm.close();
            Debug.d(TAG, "updateMerchantList: finish");
        }
    }

但是在运行时,我在此行中出错:

localMerchantRealmList.deleteAllFromRealm();

这里错误:

java.lang.UnsupportedOperationException: This method is only available in managed mode.
    at io.realm.RealmList.deleteAllFromRealm(RealmList.java:660)
    at com.myproject.customer.service.MerchantService$1.execute(MerchantService.java:107)
    at io.realm.Realm.executeTransaction(Realm.java:1393)
    at com.myproject.customer.service.MerchantService.updateMerchantList(MerchantService.java:70)
    at com.myproject.customer.service.SyncService$1.onSuccess(SyncService.java:49)
    at com.myproject.customer.api.DefaultRestClientCallback.onResponse(DefaultRestClientCallback.java:31)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:2)

使用{p>将RealmResults转换为RealmList

RealmList<Merchant> realmList = new RealmList<>();
realmList.addAll(realmResults.subList(0, realmResults.size()));

您创建了数据的非托管版本,这意味着列表中的对象不再与数据库连接,并且基本上是普通的Java对象。

您将必须保留RealmResults的引用并从中删除。