0

I am syncing using SyncAdapter, custom ContentProvider and AccountManager services. I am a bit stucked with the synchronization implementation. I have been greatly helped by the SDK example "SampleSyncAdapter" for Contacts which stores the mobile device IDs (_id in Android) in the server tables so when it responds with the dirty list, the device knows whether to add or update content.

Does this pattern mean that I have to add a new server side columns for every client? I might support other platforms in the future (e.g. iPhone data IDs - I'm not familiar with its SDK).

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Alex
  • 57
  • 5

1 Answers1

1

Use a mapping table in your server side database.

Basically:

DeviceID | DeviceItemID | ServerItemID
   Dev1        100           8912831
   Dev1        101           8819111
   Dev1        108           7717719
   Dev2        971           12091231
   ....        ...           ........
  • DeviceID would be a column that uniquely identifies the client, i.e. the specific Android phone.
  • DeviceItemID is the item identifier in the client database.
  • ServerItemID is the item identifier in the server database.

Joining this table with the server items allows you to filter out and find exactly what identifiers exists on the device.

This is the approach used in the legacy synchronization protocol OMA DS (you'd find this in Nokia phones etc.).

This would be preferable if you're going for "multi-client" synchronization, i.e. one specific data set on the server is shared among several clients - you could for instance add columns to the mapping table (such as change counters/last-modified) to allow your server to find the updated/deleted and added items & only send them.

JJD
  • 50,076
  • 60
  • 203
  • 339
Jens
  • 16,853
  • 4
  • 55
  • 52
  • Thanks for a new angle. To follow up I read this [post](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) which seems to suggest I device/installation ids are not reliable. Nevertheless is this the standard implementation for multiple devices sharing backend server db? Alex – Alex Feb 01 '12 at 16:15
  • yup, obtaining a unique device identifier is not .. totally reliable - but since you're not tracking them for advertising purposes you are free to generate your own on the client when the app is first initialized - I strongly suggest you look into using the UUID class - it provides you with reliably unique identifiers. – Jens Feb 01 '12 at 17:24
  • Thanks Jens. I didn't know about UUID class. Thinking about the mapping table... and delayed sync across devices for the same row: Should I store a client time stamp for the original transaction so as, say, an incoming 'update' op to a deleted row is ignored? I haven't done sync work before so wondered if you can see a problem with that. – Alex Feb 02 '12 at 09:26
  • Ah, the good old "Conflict resolution" part of data synchronization - but - yeah - timestamps are a good thing to exchange (preferably in UTC) so you can decide if the server or client changes win. – Jens Feb 02 '12 at 09:31