简体   繁体   中英

Update nested lists with realm

I use realm and retrofit in my Android App. I'm trying to update the JSON I get from the server. The JSON contains lists in lists. And realm wont add new data to the realm database.

First time I use realm.copyToRealmOrUpdate the correct data is added. But if I try the same a second time when the JSON contains new triggeringZones these zones wont be added. I need to reinstall the app for the zones to be added.

Anyone have a clue on how to use copyToRealmOrUpdate with lists in lists or is there any other option?

Example classes:

public class Zones {
  public List<Zone>
}

public class Deals {
  public List<AffectingZones>
}

public class AffectingZones {

}

Example JSON

{
  "transactionId": "string",
  "status": "SUCCESS",
  "statusDescription": "string",
  "triggeringZones": [
    {
      "id": 0,
      "name": "string",
      "description": "string",
      "longitude": 0,
      "latitude": 0,
      "radius": 0,
      "deals": [
        {
          "id": 0,
          "description": "string",
          "principle": "ALL",
          "affectingZones": [
            {
              "id": 0,
              "name": "string",
              "description": "string",
              "longitude": 0,
              "latitude": 0,
              "radius": 0
            }
          ],
          "triggeringZone": {
            "id": 0,
            "name": "string",
            "description": "string",
            "longitude": 0,
            "latitude": 0,
            "radius": 0
          }
        }
      ]
    }
  ]
}

copyToRealmOrUpdate

public java.util.List copyToRealmOrUpdate(java.lang.Iterable objects)

Updates a list of existing RealmObjects that is identified by their PrimaryKey or create a new copy if no existing object could be found.

Make sure to have PrimaryKey coulmn in the main table for copyToRealmOrUpdate to work.

Since Realm v1.1.0 they came up with some optimisations and now offer 4 new methods alternatively you can make use of it

  • void Realm.insert(RealmModel obj)
  • void Realm.insert(Collection collection)
  • void Realm.insertOrUpdate(RealmModel obj)
  • void Realm.insertOrUpdate(Collection collection)

These methods differ mostly from the standard Realm.copyToRealm() in the sense that they do not return any objects. This has allowed us to reduce memory allocations to almost zero as well as remove many checks that were pure overhead.

Doing this, we went from being ~40% slower than an optimized SQLite implementation to being ~70% faster for 100K objects.

Note that due to Realm's auto-update features it is possible to query for data before they are even saved, and you will get notified when the data is available. This can be a huge benefit for those of you that want to seperate displaying data from saving it:

final PersonApi api = new PersonApi();
Realm realm = Realm.getDefaultInstance();
RealmResults<Person> persons = realm.where(Person.class).findAllAsync();
person.addChangeListner(new RealmChangeListener() {
    @Override
    public void onChange(RealmResults<Person> persons) {
      if (!persons.isEmpty()) {
        // Callback when data is available
      }
  }
});

realm.executeTransactionAsync(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
      realm.insertOrUpdate(api.getPersons());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM