question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Partial update: java.lang.IllegalArgumentException: 'value' is not a valid managed object.

See original GitHub issue

Realm 3.7.1 I want to partial update my RealmObject:


public class Organization extends RealmObject {
    @PrimaryKey
    private long id;
    private Boolean isReady;
    private LocalizedString name;
    private RealmList<RealmInt> recommendations;

  public void setName(LocalizedString name) {
        this.name = name;
    }
public RealmList<RealmInt> getRecom() {
        return recommendations;
    }

    public void setRecom(RealmList<RealmInt> recom) {
        this.recommendations = recom;
    }
}

public class LocalizedString extends RealmObject {
   private RealmList<CustomString> customStringList = new RealmList<>();

   public void cascadeDelete() {
        customStringList .deleteAllFromRealm(); // The cascade part
        deleteFromRealm(); // delete this object
    }
}

public class RealmInt extends RealmObject {
    private int value;

    public RealmInt() {
    }

    public RealmInt(int val) {
        value = val;
    }
}

And method for update:
public Organization updateOrCreateOrg(final String incomingOrganizationJson) throws Exception {
        Realm realm = Realm.getDefaultInstance();
        try {
            final Organization incomingOrganization = JsonUtil.gson.fromJson(incomingOrganizationJson, Organization.class);
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    Log.d(TAG, "saveOrg: new Transaction: execute: Adding/Updating to Realm...");
                    Organization findOrganization = realm.where(Organization.class)
                            .equalTo(Organization.ID, organizationId)
                            .findFirst();
                    if (findOrganization != null) {
                        if (incomingOrganization.getName() != null) {
                            findOrganization.setName(incomingOrganization.getName()); // ERROR HERE!!!
                        }
                    } else {
                        realm.copyToRealmOrUpdate(incomingOrganization);
                    }
                }
            });

            SyncManager.getSession((SyncConfiguration) Realm.getDefaultConfiguration()).uploadAllLocalChanges();
                 return incomingOrganization;
        } catch (InterruptedException e) {
            return null;
        } finally {
            realm.close();
            Log.d(TAG, "saveOrg: FINALLY. Success close realm!"
                        + " Realm.globalCount = " + Realm.getGlobalInstanceCount(Realm.getDefaultConfiguration()));
        }
    }

Insert is execute success. But when I want to update I get error:

updateOrCreateOrg: new Transaction: execute: Adding/Updating to Realm...

updateOrCreateOrg: FINALLY. Success close realm! Realm.globalCount = 1

Error = 'value' is not a valid managed object.
java.lang.IllegalArgumentException: 'value' is not a valid managed object.
	at io.realm.OrganizationRealmProxy.realmSet$name(OrganizationRealmProxy.java:244)
	at com.myproject..model.Organization.setName(Organization.java:56)
	at com.myproject..service.OrganizationService$1.execute(OrganizationService.java:102)
	at io.realm.Realm.executeTransaction(Realm.java:1349)
	at com.myproject..service.OrganizationService.saveOrg(OrganizationService.java:63)
	at com.myproject..server.AndroidWebServer.serve(AndroidWebServer.java:82)
	at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:840)
	at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:189)
	at java.lang.Thread.run(Thread.java:841)

Error in this line:

findOrganization.setName(incomingOrganization.getName())

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Zhuindencommented, Sep 10, 2017
findOrganization.setName(realm.copyToRealm(incomingOrganization.getName())); 
3reactions
Zhuindencommented, Sep 10, 2017

Ya. For a managed object, an object you want to set as a linked object must also be a managed RealmObject as well, for example the return value of realm.copyToRealm(blah) or so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm Exception 'value' is not a valid managed object
Upon researching there is a method on realm objects to check if it is valid: realmObject.isValid();. There are two ways I know how...
Read more >
Realm (Realm 6.0.0) - MongoDB
The Realm class is the storage and transactional manager of your object persistent store. It is in charge of creating instances of your...
Read more >
Fix list for IBM WebSphere Application Server traditional V9
IBM WebSphere Application Server traditional provides periodic fixes for the base and Network Deployment editions of release V9. The following is a complete ......
Read more >
OpenIDM 2.1 > Integrator's Guide - ForgeRock Backstage
For managed objects, you can partially update an object with the patch() function. openidm.patch("managed/organization/mysampleorg", rev, value).
Read more >
Hibernate ORM 5.4.33.Final User Guide - Red Hat on GitHub
Hibernate is an Object/Relational Mapping solution for Java environments. ... the property value is generated both on insert and update.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found