Realm-java: Each element of 'value' must be a valid managed object.

Created on 9 Oct 2016  路  2Comments  路  Source: realm/realm-java

We LOVE to help with any issues or bug you have!

Questions: Caused by: java.lang.IllegalArgumentException: Each element of 'value' must be a valid managed object.
When Iuse it like this
image
image

Version of Realm and tooling

Realm version(s): 2.0.2

T-Help

Most helpful comment

At first, showMarker seems to be an managed object.
You don't need to invoke copyToRealm() against managed object since it is a method for unmanaged object.

If RealmList from getImgs() contains unmanaged object, replace it with managed object before setting the list.

So the code should be something like:

realm.beginTransaction();
RealmList<Image> list = imageShow.getImgs();
if (!list.isManaged()) { // if the 'list' is managed, all items in it is also managed
    RealmList<Image> managedImageList = new RealmList<>();
    for (Image item : list) {
        if (item.isManaged()) {
            managedImageList.add(item);
        } else {
            managedImageList.add(realm.copyToRealm(item));
        }
    }
    list = managedImageList;
}
showMarker.setImgs(list);
realm.commitTransaction();

And it's better to use executeTransaction() instead of beginTransaction()/commitTransaction().

All 2 comments

At first, showMarker seems to be an managed object.
You don't need to invoke copyToRealm() against managed object since it is a method for unmanaged object.

If RealmList from getImgs() contains unmanaged object, replace it with managed object before setting the list.

So the code should be something like:

realm.beginTransaction();
RealmList<Image> list = imageShow.getImgs();
if (!list.isManaged()) { // if the 'list' is managed, all items in it is also managed
    RealmList<Image> managedImageList = new RealmList<>();
    for (Image item : list) {
        if (item.isManaged()) {
            managedImageList.add(item);
        } else {
            managedImageList.add(realm.copyToRealm(item));
        }
    }
    list = managedImageList;
}
showMarker.setImgs(list);
realm.commitTransaction();

And it's better to use executeTransaction() instead of beginTransaction()/commitTransaction().

OK thanks

Was this page helpful?
0 / 5 - 0 ratings