java - Android Realm copyToRealmOrUpdate creates duplicates of nested objects -
i have following classes:
public class note extends realmobject { @primarykey private string id; private template template; // other primitive fields, getters & setters } public class template extends realmobject { private string name; private string color; // other primitive fields, getters & setters }
i data backend via retrofit & gson, have ready-to-use java objects in response.
let's imagine backend returns me same 3 notes each time call it. when list of note objects, following:
private void fetchnotesandsave() { list<notes> notes = getnotesviaretrofit(); realm realm = realm.getinstance(mcontext); realm.begintransaction(); realm.copytorealmorupdate(notes); realm.committransaction(); realm.close(); }
after call these lines check count of stored objects:
int notescount = mrealm.where(note.class).findall().size(); int templatescount = mrealm.where(template.class).findall().size();
for first time:
notescount == 3; templatescount == 3;
that's right. but, if call server again, same notes (same primarykey ids), , call fetchnotesandsave() again, i'll these results:
notescount == 3; templatescount == 6;
each time call copytorealmorupdate(), nested objects, inside of objects primarykey duplicated - not updated.
is there way change behaviour? please let me know if need more information. in advance!
it because template class doesn't have primary key. in case these objects inserted again there no guarantee referenced template objects safely can updated, if part of object has primary key.
if add @primarykey
template class should work expect to.
Comments
Post a Comment