java - Prevent network sync loop when syncing from network in Android ContentProvider -
i'm writing own contentprovider synced web service using syncadapter.
problem happens when sync adapter modifying content provider's data provider triggers network sync when internally calling getcontentresolver().notifychange causing sync loop.
the notifychange network sync flag required when client application modification should avoided when sync adapter modifying.
how can one, inside contentprovider, easly tell if it's being used client application (which should trigger network sync upon modification) or sync adapter (which should not trigger network sync).
currently i'm using different content_uri's (sync adapter accesses data using content_uri_no_sync , client apps using content_uri) able distinguish between 2 types of access , set network sync flag accordingly.
watch this video rest api usage in syncadapter
s.
the method discuss add set of metadata flags columns database. allows 3 things.
the flags allow
syncadapter
determine rows need changes , changes are. how tell difference between locally created row , locally modified row? furthermore how know rest api call make? if delete row, howsyncadapter
know row deleted if data gone? instead, set "should deleted" flag, , then, whensyncadapter
runs, knows push delete server.the flags allow
cursoradapter
modify view created (like addingspinner
show "this row being synced")finally, , don't point out, flags allow tell why row being modified. if none of flags set , row changes, must have been because of update server. therefore, no need sync network.
so, 2 workflows follows:
local change
- app creates new row. row "create" flag true.
- contentprovider stores row, sees create flag , calls
notifychange(...,true);
- sync network = true (the final parameter) causes
syncadapter
fire. syncadapter
scans database, finds row create flag set , performs appropriate server action. after success,syncadapter
clears flag.(row update oncontentprovivder
)contentprovider
sees flag clear, no flags left set, calls notifychange(...,false);contentobserver
s see flag change, update "sync finished"
all these steps equivalent update / delete -- 1 flag per syncable row each of create/update/delete. notice other win -- if "create" fails temporarily? server down... how know retry? -- simple, don't clear "create" flag , see 15 minutes later.
remote change
syncadapter
fires due periodic sync.syncadapter
fetches update server. pushes changes database. doesn't set flags.contentprovider
sees lack of flags, knows change must have come server (or isn't database change needs pushed server), callsnotifychange(...,false);
contentobserver
s see content change , update new row data
Comments
Post a Comment