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.

Replace exist field from json that not content this field?

See original GitHub issue

POJO:

public class Offer extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private boolean downloaded;
}

I update field “download” by true like this (click special button in my app):

public static void update(final int offerId, final boolean downloaded) {
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    Offer offer = realm.where(Offer.class).equalTo(Offer.ID, offerId).findFirst();
                    offer.setDownloaded(downloaded);
                }
            });
        } finally {
            realm.close();
        }
    }

OK. It’s work. Now field download = true. And I need this field not change any more!

But then incoming json like this (in this json no field “download”)

[
  {
    "id": 1,
    "name": "my name"
  },
  {
    "id": 2,
    "name": "my name2"
  }
  ]

I update Offer like this :

public static void updateOffers(final List<Offer> offersList) {
    Realm realm = Realm.getDefaultInstance();
    try {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmList<Offer> offersRealmList = new RealmList<>();
                offersRealmList.addAll(offersList);
                realm.copyToRealmOrUpdate(offersRealmList);
            }
        });
    } finally {
        realm.close();
    }
}

And after this update all fields “download” again = FALSE!!!

Questions: 1.This is a normal of Realm to replace exist field? (In json no this field) 2.How avoid to replace filed “download”?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
beeendercommented, Oct 30, 2017

yes, that would be a fine solution for now.

We have an issue to track this #2288

0reactions
alexei-28commented, Oct 28, 2017

Note: When client click button(download) field Offer.download = true and MUST never change!

So as result here my solution:

public class Store extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private RealmList<Offer> offers = new RealmList<>();
}


public class Offer extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private boolean downloaded;
}

Get all offers with download = true;

public static List<Offer> getoffersList(boolean downloaded) {
        Realm realm = Realm.getDefaultInstance();
        try {
            List<Offer> offersList = new ArrayList<>();
            RealmResults<Offer> realmResults = realm.where(Offer.class).equalTo(Offer.DOWNLOADED ,true).findAll();
            offersList.addAll(realm.copyFromRealm(realmResults));
            return offersList;
        } finally {
            realm.close();
        }
    }

Algorithm:

  1. offersDownloadTrueList - Get all offers with download = true
  2. Iterate list of stores and find offers that exist in list offersDownloadTrueList
  3. Replace field download by value true (by default it’s value is false)
  4. update Realm with cnanged newStoresList

newStoresList - create from incoming Json (without field “download”)

Here implementation:

 public static void updateStores(final List<Store> newStoresList) {
             Realm realm = Realm.getDefaultInstance();
        try {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    List<Offer> offersDownloadTrueList = OfferService.getoffersList(true);
                    for (Store store : newStoresList) {
                        List<Offer> newOffersList = store.getOffers();
                        for (Offer newOffer : newOffersList) {
                            if (offersDownloadTrueList.contains(newOffer)) {
                                Debug.d(TAG, "execute: Found_offer_with field 'download' =  true,  offerId = " + newOffer.getId());
                                newOffer.setDownloaded(true);
                            }
                        }
                    }

                    RealmList<Store> storesRealmList = new RealmList<>();
                    storesRealmList.addAll(newStoresList);
                    realm.copyToRealmOrUpdate(storesRealmList);
                }
            });
        } finally {
            realm.close();
        }
}

And now in Realm offers that has field download = true not change. OK. It’s work. Fine.

The question is: Is this a best solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Removing a field from jSon String - Stack Overflow
This above object is a dynamic object returned as string I want to remove Violation related fields and its values from it. I...
Read more >
Replace op should error out if path doesn't exist #119 - GitHub
Hi, As per Json RFC, path must exist for the operation to be successful. ... of this repository and replace does indeed fail...
Read more >
Modifying JSON data using JSON_MODIFY() in SQL Server
Suppose we specify a property in the path argument that does not exist, in this case, the JSON_MODIFY function tries to insert the...
Read more >
Working with Json fields (Concepts) - Prisma
You need to store data that does not have a consistent structure ... JsonObject utility classes to work with the contents of a...
Read more >
Validate, Query, and Change JSON Data with Built-in Functions
Validate JSON text by using the ISJSON function. The ISJSON function tests whether a string contains valid JSON. The following example returns ...
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