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

Related issues

wyvern610 picture wyvern610  ·  3Comments

gpulido picture gpulido  ·  3Comments

harshvishu picture harshvishu  ·  3Comments

pawlo2102 picture pawlo2102  ·  3Comments

yuwu picture yuwu  ·  3Comments