createOrUpdateAllFromJson, removing object that not exist anymore in Json
See original GitHub issueGoal
Update Realm Database with “createOrUpdateAllFromJson” without lost reference to other objects but removing object that not exist anymore in Json
Expected Results
Realm Database with only RealmObjects contained in Json (so RealmObjects no longer exist not should be in Database).
Actual Results
Realm Database with RealmObjects contained in Json and also RealmObjects no longer exist in Json.
Steps & Code to Reproduce
Using createOrUpdateAllFromJson from Realm Android Documentation I read:
Parsing JSON with Realm is subject to the following rules.
- Creating object with JSON which has the field with a null value:
- For a not-required field, set it to null which is the default value.
- For a required field, throw an exception.
- Updating object with JSON which has the field with a null value:
- For a not-required field, set it to null.
- For a required field, throw an exception.
- JSON doesn’t have the field:
- Leave the value unchanged for both required and not-required fields.
After I execute Code Sample my database contains all previous RealmObjects and also objects that no longer exist in Json.
Is there a way to achieve removal that objects?
Code Sample
Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createOrUpdateAllFromJson(Sector.class, sectorsOffline);
}
});
Version of Realm and tooling
Realm version(s): 3.1.3
Realm sync feature enabled: no
Android Studio version: 2.3.1
Which Android version and device: Emulator and real devices with Android SDK 25
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
In the end, I choose to modifying api so I can use
createOrUpdateAllFromJson()
and don’t do more work on app. Now I design json to obtain all objects and, with a parameter “isDeleted” I know if object is deleted or not.I thought a parameter could be useful in the method
createOrUpdateAllFromJson()
to achieve this behaviour instead of doing if manually 😉@geolyth Thanks for sharing. It sounds like something we should describe under “Recipes” in our documentation.