java - Only valid managed objects can be copied from Realm -


i have one-to-many relationship in realm

public class bayentity extends realmobject implements realmmodel {      @primarykey     private int     id;     private string  title; }  public class termentity extends realmobject implements realmmodel {      @primarykey     private string termid;     private string name;      private realmlist<bayentity> baylist;   public void updatebaylist(arraylist<bayentity> baylist) {          if(collectionutility.isemptyornull(this.baylist))             this.baylist = new realmlist();         this.baylist.addall(baylist);         realm.begintransaction();         realm.copytorealm(this.baylist);         realm.insertorupdate(this); //update current instance         realm.committransaction();     }      public arraylist<bayentity> getsimplelist() {         if(collectionutility.isemptyornull(baylist))             return new arraylist<>();         return (arraylist<bayentity>) realm.copyfromrealm(baylist);     } } 

i getting termentity objects containing baylist as:

public realmresults<termentity> getterms() {         return realm.where(termentity.class).findall();     } 

i able save , retrieve baylist successfully. when try convert arraylist in method getsimplelist(), getting exception:

java.lang.illegalargumentexception: valid managed objects can copied realm.

i can't understand how objects can unmanaged when saved , retrieved realm. please correct me. thanks.

it looks me though have several problems:

  1. where getting realm instance use in updatebaylist method? since instance local state, might refer realm not valid in current context (time or thread)

  2. the first 3 lines if ... addall(baylist); modify terms realmlist. can't outside of transaction, if object managed. when object not managed, error describe, because trying add managed objects unmanaged realmlist

  3. while not problem, realmobject implements realmmodel. once, why again?

edited add:

in order add bayentitys termentity, first make sure termentity managed object (use copytorealm, or query it). once that, code in updatebaylist correct. want like:

    realm.begintransaction();     if (collectionutility.isemptyornull(this.baylist)) {         this.baylist = new realmlist();     }     this.baylist.addall(baylist);     realm.committransaction(); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -