java - Google IO 2010 Virgil Dobjanschi pattern C example -
i have tried virgil dobjanschi patterns , b , made conclusion want use pattern c because in case can use google based code take care how may data device if have non-stable network connection , on... didn't find how implement in right way. when consider database large enough syncing every time.
i did this
when cursorloader did query show bizinbox query returned cursor , started syncadapter should data , stop activity
but problem syncadatert's onperformsync start agin , again. , looks endless cycle when cursor reload data , start onperformsync , on... , syncadapter in parallel without queue hoped.
i should notice rest end process data correctly , second request onperformsync have no data because used if-modified-since http header.
it did wrong, becuase syncadapter did not sync backoff , did requests without end.
when did if (!contentresolver.issyncactive(mconnectedaccount, scheme.authority)) { contentresolver.requestsync( mconnectedaccount, // sync account scheme.authority, // content authority b); // extras } else { log.d(tag, "> runsyncadaterforclaimbizinbox skip starting adapter active"); }
i able win problem question remained how perform syncing if need sync part of data - not data contentresolver.issyncactive(mconnectedaccount, scheme.authority) - method can not give info part of data syncing , don't have queue reasons here. how manage problem ?
please, me find errors or may has pattern c compilable code find out how implement pattern right.
thanks in advance.
the code below:
public class contentprovider {
@override public cursor query(final uri uri, final string[] projection, final string selection, final string[] selectionargs, final string sortorder) { final sqlitequerybuilder qb = new sqlitequerybuilder(); final string orderby = preparequery(uri, sortorder, qb); final sqlitedatabase db = databasehelper.getreadabledatabase(); final cursor cursor = qb.query(db, projection, selection, selectionargs, null, null, orderby); runsyncadapterforquery(uri); cursor.setnotificationuri(getcontext().getcontentresolver(), uri); return cursor; } void runsyncadapterforquery(uri uri) { switch (urimatcher.match(uri)) { case biz_inbox: runsyncadaterforbizinbox(method_get); break; } } void runsyncadaterforbizinbox(string method) { bundle b = new bundle(); b.putboolean(contentresolver.sync_extras_manual, true); b.putboolean(contentresolver.sync_extras_expedited, true); b.putstring(method_extra, method); b.putint(resource_type_extra, resource_type_biz_inbox); account mconnectedaccount = new account(bpalaccountgeneral.account_name, bpalaccountgeneral.account_type); //if (!contentresolver.issyncactive(mconnectedaccount, scheme.authority)) { need ? sync adapters queue ? contentresolver.requestsync( mconnectedaccount, // sync account scheme.authority, // content authority b); // extras //} }
}
syncadapter @override public void onperformsync(account account, bundle extras, string authority, contentproviderclient provider, syncresult syncresult) { string accesstoken = "some_token"; switch (resourcetype) { case unknown_resource_type: //do update ... break; case resource_type_biz_inbox: if (method.equals(method_get)) { string url = "some_inbox_url" getbizinboxrequest request = new getbizinboxrequest(); bizinboxdao bizinboxdao = new bizinboxdao(); string lastmodified = bizinboxdao.getlastmodified(getcontext().getcontentresolver()); restmethodresult<restlistentitycontainer<bizinboxentity>> restmethodresult = request.getbizinboxitems( url, null, lastmodified, accesstoken); int statuscode = restmethodresult.getstatuscode(); if (statuscode == mobileconstant.http_ok) { list<bizinboxentity> serverbizinboxentities = restmethodresult.getresource().getlist(); (bizinboxentity entity : serverbizinboxentities) { bizinboxdao.insertydata(getcontext().getcontentresolver(), entity); syncresult.stats.numinserts++; } if(syncresult.stats.numinserts > 0) { log.d(tag, "> onperformsync bizinboxdao.sendnotification "); bizinboxdao.sendnotification(getcontext().getcontentresolver()); } } else { syncresult.stats.numioexceptions++; syncresult.databaseerror = true; } } break; default: break; } }
i did big mistake using code above.
according pattern c sync adapter should check current data state. shouldn't call request sync directly content provider. ways able use described in android docs https://developer.android.com/training/sync-adapters/running-sync-adapter.html
if called syncing below // disable sync backoff , ignore sync preferences. in other words...perform sync now! b.putboolean(contentresolver.sync_extras_manual, true); b.putboolean(contentresolver.sync_extras_expedited, true);
account mconnectedaccount = new account(accountgeneral.account_name, accountgeneral.account_type); contentresolver.requestsync( mconnectedaccount, // sync account scheme.authority, // content authority b); // extras
you should remember put comments:
disable sync backoff , ignore sync preferences. in other words...perform sync now!
it means request not try repeat if failed. , code run action in parallel. example if run requestsync 2 tiles have 2 syncing threads found.
and last main note: when run requestsync query caused endless cycle, because after requestsync cursot notifing call re-query run requestsync again , etc...
- i found example app used pattern c - link: https://github.com/taoliuh/v2ex
Comments
Post a Comment