Hi,
The createOrUpdateAllFromJson method or similar other methods,
support for importing objects by putting only id for relation objects,
instead of putting whole object data on the json file.
For example when we have these two Dog and Person models:
public class Dog extends RealmObject {
@PrimaryKey
private int id;
private String name;
}
public class Person extends RealmObject {
@PrimaryKey
private int id;
private String name;
private RealmList<Dog> dogs;
}
And assumed we currently have these records on the Dog model on the database:

Now, we want to able to import Person model from this Person.json file:
[{"id":1, "name": "Jane", "dogs": [1,2]]
Instead of this Person.json file:
[{"id":1, "name": "Jane", "dogs": [{"id": 1, "name": "Fido"}, {"id": 2, "name": "Fluffy"}]
by doing:
InputStream stream = getAssets().open("Person.json");
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Person.class, stream);
realm.commitTransaction();
Hi,
The
createOrUpdateAllFromJsonmethod or similar other methods,support for importing objects by putting only
idfor relation objects,instead of putting whole object data on the json file.
For example when we have these two
DogandPersonmodels:And assumed we currently have these records on the
Dogmodel on the database:Now, we want to able to import
Personmodel from thisPerson.jsonfile:[{"id":1, "name": "Jane", "dogs": [1,2]]Instead of this
Person.jsonfile:[{"id":1, "name": "Jane", "dogs": [{"id": 1, "name": "Fido"}, {"id": 2, "name": "Fluffy"}]by doing: