uploadDirty() you call
prepareUpload() and then
remote.put() on a dirty event. If it succeeds, you then call
clearDirty() so that you won’t try to upload it again. The problem with this is that another process in the Android device can modify the event after you have called
prepareUpload(). This will set the dirty flag (not documented, but I looked in CalendarProvider source code and it does), but you clear it in
clearDirty(), so you will never synchronise the new modification until someone changes that event again.
It is fairly improbable that the user modifies a calendar event just as you are synchronising, but if some app makes a lot of modifications to calendar events, the first modification will start a sync, so subsequent modifications may well happen while you are syncing.
I think that the correct logic is to call
prepareUpload(). Of course if the upload fails you then have to set the dirty bit again, so you have to enclose all the code from
remote.put() in a
try block which catches all exceptions. This can still fail if Android crashes or if your process gets summarily terminated by the OOM killer, but these are very unlikely events and it is theoretically impossible to design a protocol that is proof against these failures anyway.
I would offer a patch if it was Java code, but I’m not familiar enough with Kotlin to write code in it.