将对象复制到Realm时出错

时间:2016-09-29 11:47:17

标签: android realm

我正在尝试更新RealmObject,但我一直收到以下错误日志:

Cannot copy an object from another Realm instance.

java.lang.IllegalArgumentException: Cannot copy an object from another Realm instance.
    at io.realm.RealmList.copyToRealmIfNeeded(RealmList.java:261)
    at io.realm.RealmList.add(RealmList.java:190)
    at com.radioafrica.music.activity.AudioPlayer$1.execute(AudioPlayer.java:193)
    at io.realm.Realm$1.run(Realm.java:1187)
    at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

更新对象的代码是:

realm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    Favorite favorite = realm.where(Favorite.class).findFirst();

                    favorite.getTracks().add(getCurrentTrack());
                    realm.copyToRealmOrUpdate(favorite);
                }
            }, new Realm.Transaction.OnSuccess() {
                @Override
                public void onSuccess() {
                    Toast.makeText(AudioPlayer.this, "Added to favorites", Toast.LENGTH_SHORT)
                            .show();
                }
            }, new Realm.Transaction.OnError() {
                @Override
                public void onError(Throwable error) {
                    Log.e(TAG, error.getMessage(), error);
                }
            });

0 个答案:

没有答案