performance - In which cases should I use SQLite on Android to cache data? -
so, write client video platform.
let's see @ 1 of features of app: fetching video.
now, use rxjava + retrofit fetch video , display it.
below code without caching:
retrofit api:
@get("/api_v3/?service=media&action=list&pager%3aobjecttype=kalturafilterpager&pager%3apagesize=500&pager:pageindex=1") observable<kalturavideoresponse> getvideolistobservable();
and usage in activity:
api.getvideolistobservable() .doonerror(t -> t.printstacktrace()) //handle error .map(r -> r.getobjects()) //get business objects response .doonnext(l -> videoactivity.this.runonuithread(() -> fragment.updatevideolist(l))) //update ui .doonnext(l -> kalturavideolist.addall(l)) //save objects memory .subscribeon(schedulers.io()) //do in background thread .observeon(androidschedulers.mainthread()) //handle result in ui thread .subscribe();
advantages:
- very simple code
- simple handling exceptions
- simple data manipulations (using rxjava operators)
disadvantages:
- at every sreen rotation new httprequest
- a lot of anonymouse classes
- if frequently, oom on not top devices. example, on alcatel 1 touch 4033d
and other example. using syncadapter + contentprovider. not add code of syncadapter configuration, add code, responsible video synchroniztion
private void updatevideolist(){ kalturavideoresponse videolist = api.getvideolist(); getcontext().getcontentresolver().delete(kalturavideocolumns.content_uri, null, null); for(kalturavideo video : videolist.getobjects()){ final kalturavideocontentvalues values = new kalturavideocontentvalues(); values .putcategories(video.getcategories()) .putcategoriesids(video.getcategoriesids()) .putdataurl(video.getdataurl()) .putdescription(video.getdescription()) .putname(video.getname()) .putdownloadurl(video.getdownloadurl()) .putduration(video.getduration()) .putkalturaid(video.getid()) .putthumbnailurl(video.getthumbnailurl()); getcontext().getcontentresolver().insert(kalturavideocolumns.content_uri, values.values()); } } getcontext().getcontentresolver().insert(kalturavideocolumns.content_uri, values.values()); } }
this function called every time, when syncadapter synchronizing. @ configuration every 30 min.
and getting data @ activity(using cursorloader):
@override public loader<cursor> oncreateloader(int i, bundle bundle) { final string sortorder = kalturavideocolumns._id + " desc"; return new cursorloader( getactivity().getapplicationcontext(), kalturavideocolumns.content_uri, null, null, null, sortorder); } @override public void onloadfinished(loader<cursor> loader, cursor cursor) { kalturavideocursor videocursor = new kalturavideocursor(cursor); if(videocursor.movetofirst()){ videoadapter = new videoadapter(getactivity(), cursor, true); viewholder.videolist.setadapter(videoadapter); viewholder.videolist.setonitemclicklistener(onitemclicklistener); } }
at code fetching videos, cached syncadapter , display using videoadapter.
advantages:
- i don't lot of httprequests, every 30 mins.
- simple activity lifecycle handling
- very fast loading , displaying data
disadvantages:
there 2 fatal disadvantage.
if video edited or deleted, don't know it.
if frequent synchronization(for example every 3 min), app works slow...
how can fix problem?
when start learning android, found beautiful android course on udacity. in course developed sunshine app. , in app used syncadapter, cached data in sqlite. in sunshine app data weather, updates not frequently.
need frequent synchronization see, if changes occured. need improve caching/synchronization code, or should request every time when need data?
Comments
Post a Comment