java - Realm for Android, how to sync server data to realm database? -
i'm trying sync city
list
public class city extends realmobject { @primarykey @serializedname("id") private int id; @serializedname("title") private string title; @serializedname("clubs") private realmlist<club> clubs; }
that contains clubs, , club
contains various other models. there simple way can make local realm database identical response, if city not returned in json, need remove realm, otherwise update it. current thoughts 2 iterations, 1 update , create new city instances, other remove unneeded ones.
for example, updating user info server, when user has read news saved locally, troublesome is:
private void createorupdateuser(user updated) { user user = mrealm.where(user.class).equalto("id", updated.getid()).findfirst(); mrealm.begintransaction(); realmlist<realmint> removednews; if (user != null) { removednews = new realmlist<>(); removednews.addall(user.getremovednews()); } else { removednews = new realmlist<>(); } updated = mrealm.copytorealmorupdate(updated); updated.setremovednews(removednews); mrealm.committransaction(); }
if don't wrap relations arraylist
/realmlist
, re-set on updated entry, copytorealmorupdate
removes them , there's no way them back.
updated.setremovednews(user.getremovednews());
has same outcome, when copytorealmorupdate
executed, updated user has 0 read news. same goes when replace realmint
news
model thas has @primarykey
.
retrofit2, gson , realm used.
Comments
Post a Comment