ginatrapani/todo.txt-touch-ios — pull request #83 by chuckbjones (2011-12-12 07:35:31+00:00)
Title: implemented changes to support new revision support in Dropbox API v1.0

Alright. So, Dropbox really doesn't want people to accidentally overwrite their files (which is a good thing). If you call uploadFile and pass nil in the parentRev parameter, a new file will be created: 'todo (1).txt'. If you pass a parentRev that is not the latest revision of the file, a different file is created: 'todo (Joe User's conflicted copy).txt'. In order to overwrite the existing file, we have to pass the current revision number in the parentRev parameter.

So here's what I had to do to get this to work:

First, before downloading we check the latest revision of the todo.txt file on dropbox. Then, we download that revision of the file and save off the revision number. We have to do it in that order because there is no way to get the revision number after downloading the file. Otherwise, if someone changed the file from the desktop or another app between our download and metadata request we would get a different revision number.

Now, when we get ready to upload we request the metadata again and compare the latest revision number to our saved value. If they match, we go ahead and upload. If they don't match (or if we uploaded the file and got a different file name back in the metadata) then we display an error message to the user and prompt him to upload or download similar to the manual sync dialog.

There is still a race condition here where the file could be modified on dropbox between the time where we request the metadata and when we actually upload the file. But that should rarely (if ever) happen, and we should get an error response back from dropbox and alert the user. I still need to test that case.

I also added a flag that gets set when an upload fails or is cancelled so that the manual sync dialog is displayed on the next sync.