我使用realm(0.88.2)和reformation2的组合来检索一些对象。当我尝试使用“copytorealmorupdate”将对象复制/更新到我的领域时,将抛出一个IllegalargumentException,并显示消息“cannot copy an object from another realm instance”。
我改装电话的回复是:

    public void onResponse(Call<List<Werkbon>> call, Response<List<Werkbon>> response) {
        if (response.isSuccess()){
            Realm realm = Realm.getDefaultInstance();
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(response.body());
            realm.commitTransaction();
            realm.close();
        }
    }

我还尝试过在response.body()上使用for循环,尝试逐个添加对象,结果是相同的错误/消息。
以及引发的错误:
03-21 15:32:25.754 27465-27465 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.vl.test, PID: 27465
    java.lang.IllegalArgumentException: Cannot copy an object from another Realm instance.
    at io.realm.RealmList.copyToRealmIfNeeded(RealmList.java:246)
    at io.realm.RealmList.add(RealmList.java:181)
    at io.realm.WerkbonRealmProxy.copy(WerkbonRealmProxy.java:732)
    at io.realm.WerkbonRealmProxy.copyOrUpdate(WerkbonRealmProxy.java:696)
    at io.realm.DefaultRealmModuleMediator.copyOrUpdate(DefaultRealmModuleMediator.java:330)
    at io.realm.Realm.copyOrUpdate(Realm.java:1395)
    at io.realm.Realm.copyToRealmOrUpdate(Realm.java:765)
    at io.realm.Realm.copyToRealmOrUpdate(Realm.java:814)
    at com.example.app.datamodel.werkbon.Werkbon$1.onResponse(Werkbon.java:81)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:66)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

问题:为什么会抛出这个错误,我如何才能将这些对象带入我的领域?

最佳答案

beeender的帮助下回答我自己的问题。
我的Werkbon对象包含一个RealmList对象。realmlong只是一个长值的容器。
在改型中,我添加了一个typeadapter来将RealmLong转换为这些longs。在这个适配器中,我已经将realmlong添加到域中,如下所示:

    mGson = new GsonBuilder()
            .registerTypeAdapter(RealmLong.class, new RealmLongDeserializer())
            ...
            .create()

以及typeadapter本身,其中realmlongs被添加到一个领域:
// Don't copy this, wrong version! Scroll down for correct TypeAdapter
private class RealmLongDeserializer implements JsonDeserializer<RealmLong> {

    @Override
    public RealmLong deserialize(JsonElement jsonElement, Type typeOF,
                            JsonDeserializationContext context) throws JsonParseException {
        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        RealmLong rl = realm.createObject(RealmLong.class);
        rl.setVal(jsonElement.getAsLong());
        realm.commitTransaction();
        realm.close();
        return rl;
    }
}

不过,我还不必将其添加到领域中,因为RealmLongscopyToRealmOrUpdate函数已经在后期执行了此操作。因此,typeadapter可以如下所示:
private class RealmLongDeserializer implements JsonDeserializer<RealmLong> {

    @Override
    public RealmLong deserialize(JsonElement jsonElement, Type typeOF,
                            JsonDeserializationContext context) throws JsonParseException {
        RealmLong rl = new RealmLong(jsonElement.getAsLong());
        return rl;
    }
}

10-04 19:32