Skip to content

Support for importing relation objects by ID from json file #4627

@omidraha

Description

@omidraha

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:

table-diagram

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();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions