question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createOrUpdateAllFromJson, removing object that not exist anymore in Json

See original GitHub issue

Goal

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.

  1. 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.
  2. 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.
  3. 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
geolythcommented, Apr 25, 2017

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 😉

0reactions
knethcommented, Apr 26, 2017

@geolyth Thanks for sharing. It sounds like something we should describe under “Recipes” in our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm Android - createOrUpdateAllFromJson, obtain objects ...
Is there a way to obtain list of objects not contained in json ... to other objects but removing object that not exist...
Read more >
How to delete an index from JSON Object ? - GeeksforGeeks
Go to the directory where your JSON file is saved by using cd command. Now simply type command. node file_name. Output: Using delete...
Read more >
JSON - JavaScript - MDN Web Docs
The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON. It can't be called or constructed.
Read more >
Realm (Realm 10.10.1) - MongoDB
Creates a Realm object pre-filled with data from a JSON object. This must be done inside a transaction. JSON properties with unknown properties...
Read more >
In JSON, how to remove "Data" and "Item" - Forums - IBM
Once we add the 'Item's, we can now very easily convert to XML. However, these names are not always the desired values in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found