in migration android, add new field from table in database
schema.get("RealmUserInfo")
.addRealmObjectField("userInfo_temp", valueSchema)
.transform(obj -> {
JsonObject jsonObject = new JsonParser().parse(obj.getString("historyUserInfo")).getAsJsonObject();
if (jsonObject != null) {
Info info = Utils.jsonToObject(Info.class, jsonObject.getAsJsonObject("user").toString());
DynamicRealmObject valueObj = obj.get("userInfo_temp");
valueObj.setInt("id", info.getId());
valueObj.setString("firstName", info.getFirstName());
valueObj.setString("lastName", info.getLastName());
valueObj.setString("email", info.getEmail());
valueObj.setString("phone", info.getPhone());
valueObj.setString("fbId", info.getFbId());
valueObj.setString("referralCode", info.getReferralCode());
valueObj.setInt("numberOrders", info.getNumberOrders());
valueObj.setString("lastOrderDate", info.getLastOrderDate());
valueObj.setInt("creditAvailable", info.getCreditAvailable());
valueObj.setInt("freeDeliveryDays", info.getFreeDeliveryDays());
valueObj.setString("freeDeliveryExpirationDate", info.getFreeDeliveryExpirationDate());
valueObj.setInt("freeDeliveryNextOrder", info.getFreeDeliveryNextOrder());
valueObj.setInt("canRefer", info.getCanRefer());
valueObj.setString("oauthVerifier", info.getOauthVerifier());
valueObj.setInt("orderIdToScore", info.getOrderIdToScore());
valueObj.setString("imageFacebook", info.getImageFacebook());
valueObj.setInt("phoneValidated", info.getPhoneValidated());
valueObj.setString("phoneValidatedMessage", info.getPhoneValidatedMessage());
valueObj.setBoolean("hasVisaPurchases", info.getHasVisaPurchases());
valueObj.setInt("sendAdvertising", info.getSendAdvertising());
valueObj.setInt("isNew", info.isNew());
valueObj.setList("creditCards", info.getCreditCards());
valueObj.setList("directions", info.getDirections());
}
})
.removeField("userInfo")
.renameField("userInfo_temp", "userInfo");
in this line DynamicRealmObject valueObj = obj.get("userInfo_temp"); object is null, why this ?
Because you've just created the field and default value is null.
You're probably looking for dynamicRealm.createObject(clazz)
@Zhuinden in realm it's not allowed to have two primary keys with the same value ?
Well I mean it's the same object, no?
Generally you want something like
MyObj myObj = realm.where(MyObj.class).equalTo("id", 5L).findFirst();
if(myObj == null) myObj = realm.createObject(MyObj.class, 5);
@Zhuinden thanks!
I have a new question, in my model I have fields types Double
var latitude: Double = 0.toDouble()
in migration have this exception
'Direction.latitude' has been made required.
@Zhuinden thanks for your help
@Zhuinden why this exception ?
java.lang.IllegalArgumentException: RealmList must contain DynamicRealmObject's, not Java model classes.
Oh right, you're trying to do a migration.
Then it's the same concept but instead of
MyObj myObj = realm.where(MyObj.class).equalTo("id", 5L).findFirst();
if(myObj == null) myObj = realm.createObject(MyObj.class, 5);
you need
DynamicRealmObject myObj = realm.where("MyObj").equalTo("id", 5L).findFirst();
if(myObj == null) myObj = realm.createObject("MyObj", 5);