android - RealmList inside RealmObject not being populated from json -
so started playing around realm , been pretty self-explanatory simple use cases far i've found creating realm object realmlist inside json doesn't populate realm list. here's i've got:
public class user extends realmobject{ @primarykey private int user_id; private realmlist<place> places; private string fname; private string lname; private string birth_date; public realmlist<place> getplaces(){ return this.places; } public void setplaces(realmlist<place>places) { this.places = places; } } public class place extends realmobject{ private string place_name; //several other types ints , strings getters , setters }
both of these classes have appropriate getters , setters in actual code included sample of information , of important info shorten this.
i using retrofit , of data coming in jsonelements.
userservice.requestprofile(new callback<jsonelement>() { @override public void success(jsonelement profileresponse, response response) { log.d(tag, profileresponse.tostring()); //shows raw response containing multiple places objects realm.begintransaction(); user user = null; try { user = (user)realm.createobjectfromjson(user.class, profileresponse.tostring()); }catch (realmexception re){ re.printstacktrace(); } if(user != null) { log.d(tag, user.getfname()); //comes out correctly log.d(tag, user.getplaces().size()) //always says 0 } realm.committransaction(); } @override public void failure(retrofiterror error) { error.getcause(); } });
any idea why i'm not seeing when call getplaces on user? have tried embedding realm objects in realm objects , seems fine, realmlist seems give me issue. i'm not sure if data being saved realm in first place when createobject called. tried createallfromjson
could not create json array string
exception edit: example json {"places":[{"place_id":1280,"place_name":"canada"}}]}
i suggest use gson deal json. have used gson, retrofit , realm following implementation.
build gson exclusion strategy
gson gson = new gsonbuilder().setexclusionstrategies(new exclusionstrategy() { @override public boolean shouldskipfield(fieldattributes f) { return f.getdeclaringclass().equals(realmobject.class); } @override public boolean shouldskipclass(class<?> clazz) { return false; } })
add gson restadapter builder
restadapter.builder() //.other settings .setconverter(new gsonconverter(gson)) .build();
Comments
Post a Comment