Search code examples
androidandroid-calendarandroid-syncadapter

Detect concurrent event edits


I am writing a SyncAdapter to synchronize a calendar, and am wondering how I can detect edits that are concurrent to the synchronization. I've not found anything in the docs about this case.

Example: 1) the SyncAdapter gets a cursor from the calendar provider with all the dirty events, 2) syncs them and 3) resets the dirty flags. Between 1) and 3), the user modifies one of the dirty events. This modification will not be synced, because the dirty flag is reset after the edit!

I know that the contacts provider has a version field that can be used for this. What about the calendar provider?


Solution

  • Here is a "hacky" solution (I didn't try this myself).

    Before you do step 1, you replace the value of the DIRTY field with 2 (or any value other than 0 and 1) for all rows which have DIRTY = 1.

    i.e, in SQL this would be something like

    update events set DIRTY = 2 where DIRTY = 1 and account_type = 'account type'  and account_name = 'account name';
    

    Then you only sync those which have DIRTY = 2.

    When you reset the DIRTY flag, you only do that for those which still have DIRTY = 2 as well.

    If an event has DIRTY = 1, it was modified in between and you need to start over.

    The CalendarProvider sets DIRTY = 1, whenever a non-sync-adapter call modifies an event. You can use this to detect any changes while you were syncing and only "acknowledge" those evens which were not modified.