realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

createOrUpdateFromJson also updates fields not set

Open cmelchior opened this issue 11 years ago • 4 comments

If you update a existing RealmObject using Realm.createOrUpdateFromJson() it correctly creates default values for fields not set, however if a existing RealmObject is found the expected behavior would be to only override the fields actually set, eg

public class Data extends RealmObject {
  @PrimaryKey
  private int id;
  private boolean bool;
  private String str;

  ...
}

// From empty database
realm.beginTransaction();
Data obj = realm.createOrUpdateFromJson(Data.class, "{ "id":1, "str": "foo" });
obj.setBool(true);
realm.commitTransaction();

realm.beginTransaction();
obj = realm.createOrUpdateFromJson(Data.class, "{ "id":1, "str": "foo" })
realm.commitTransaction();

obj.isBool() == false; // unexpected, it should maintain its value unless specifically set.

cmelchior avatar Mar 06 '15 18:03 cmelchior

This is also the case when using: copyToRealmOrUpdate(E extends RealmObject)

cornflakes avatar Mar 06 '15 22:03 cornflakes

Actually, when using copyToRealmOrUpdate, you have an existing object. It is impossible to tell the difference between an value not set and it's default value, so there it should override all properties. This is not the case for JSON objects where the field definition might miss altogether.

cmelchior avatar Mar 07 '15 06:03 cmelchior

Point taken :+1:

cornflakes avatar Mar 08 '15 19:03 cornflakes

.

bmunkholm avatar Mar 10 '15 07:03 bmunkholm