android - RealmObject is not valid, so it cannot be copied -
why code triggering a
java.lang.illegalargumentexception: realmobject not valid, cannot copied
error?
realm.begintransaction(); realm.delete(booking.class); final booking booking = realm.copyfromrealm( new booking( editfragment.getdestination(), editfragment.getdate())); realm.committransaction();
however, if change exception not thrown:
realm.delete(booking.class); final booking booking = realm.createobject( booking.class, uuid.randomuuid().tostring()); booking.setdestination(editfragment.getdestination()); booking.setdate(editfragment.getdate());
what difference?
the booking
class defined as:
public class booking extends realmobject { @required @primarykey private string id; @required private string destination; @required private date date; private int status; private boolean isnotified; public booking() { this.id = uuid.randomuuid().tostring(); this.status = status_unavailable; } public booking(string destination, date date) { this(); this.destination = destination; this.date = date; } // getters , setters follow }
edit:
found wrong: accidentally used copyfromrealm
instead of copytorealm
. looks autocomplete works against if not careful enough haha.
you have pass valid realmobject copyfromrealm(..) not sufficient create new instance on fly, because new object won't attached realm.
makes standalone in-memory copy of already persisted {@link realmobject}.
in second example creating new object , passing db persisted: 2 different operations.
Comments
Post a Comment