I want to sync my android device's contacts with a server (1-way syncing: update server according to the device's contacts DB).
I've seen the SampleSyncAdapter example: http://developer.android.com/resources/samples/S开发者_运维百科ampleSyncAdapter/index.html
and a very good blog for understanding the pieces in the puzzle: http://ericmiles.wordpress.com/2010/09/22/connecting-the-dots-with-android-syncadapter/
I understand that the sync logic itself is supposed to be in overriding onPerformSync() in a class extending AbstractThreadedSyncAdapter.
I was not, however, able to find an example for the syncing algorithm. For instance, how do I sync just the diffs from the previous sync? am I supposed to keep a cache for the latest synced contacts and diff it with the current contacts DB?
Ideally, you offload much of that work to the server.
If you're talking about a REST api type of server, you have two options.
- First, go fetch an index of the rows in your dataset, and pass a parameter as part of the request to filter out rows changed since a date, and then fetch those specific rows only.
If that isn't supported by their REST API,
- Fetch each row at its own URL, and use HTML header fields to identify only what's changed. See "If-Modified-Since" field for example. This will tell the server that if the page you fetch is unchanged, not to bother returning it, but rather to send a 304 (not modified) response. If it's 304, you know you don't need to change your DB.
精彩评论