0

I have a problem in inserting objects inside a realmlist. It is duplicating my objects inside Realm, and they have the very same primary key.

class ParentObj extends RealmObject {
   RealmList<SomeObject> objects;
}

class SomeObject extends RealmObject {
   @PrimaryKey
   @Required
   String id;
   ...
}

when I get a list of SomeObject like:

List<SomeObject> objs = ...;
User.getObjects().addAll(objs);

my RealmList (objects) gets duplicated. I've made sure it is the same primary key. Anyone has any idea of what is happenning?
Thank you!

reinaldomoreira
  • 5,388
  • 3
  • 14
  • 29

1 Answers1

1

A RealmList works just like an ArrayList, so the same item can be there multiple times. If you want to update SomeObject you should just do that directly. The objects RealmList will reflect those changes.

It isn't clear exactly what you are trying to do, so from the given information it is hard to give more advice.

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • I get all objects at my webservice and update my local objects inserting it to my realmlist. I was expecting to not duplicate them when doing this. Otherwise I would have to always search for my object and update each one of them everytime I make a webservice call. – reinaldomoreira Oct 09 '17 at 17:51
  • You don't need to manipulate the `objects` array to update `SomeObject`, just search for and update, `SomeObject` directly. The changes will be reflected in the `objects` array. – Christian Melchior Oct 09 '17 at 17:54
  • if I understood correcly, I could do a Realm.copyToRealmOrUpdate in the list `objs`, and the changes will be reflected in my `objects`. Is that correct? – reinaldomoreira Oct 09 '17 at 18:15
  • Yes, that is correct. It is the same mental model as working with a pure Java hierarchy where having a primary key means it is a singleton object. – Christian Melchior Oct 09 '17 at 18:17