Question

I'm having trouble understanding what my options are to merge 2 or more conflicted versions of a UIDocument stored in iCloud. After detecting the document conflict, I currently use removeOtherVersionsOfItemAtURL to simply keep the latest version of the document's data. However, I would like to support merging conflicted documents.

The only resources and tutorials I found referred to Core Data and persistent stores for merging, and the examples used things like SQLite, which I don't use. In my case, each document is a separate file.

Background: My app uses custom NSObject subclasses (with relevant of properties) to store data. They are encoded and decoded (using NSCoding + NSKeyedArchiver/NSKeyedUnarchiver) inside of a UIDocument. I have set up my app to fully work with iCloud. It can make files ubiquitous and vice-versa, edit and delete them.

Please help me to understand what I can do regarding merging conflicted UIDocument's!

Was it helpful?

Solution

You'll use NSFileVersion to get conflicting versions of the file. When you get into a conflict state, the +unresolvedConflictVersionsOfItemAtURL will give you NSFileVersion instances for any versions not considered "current". You can then use the URL property of NSFileVersion to get the contents of those conflicting versions.

Now you have more than one version of the document. How you resolve the conflict is up to you-- go through the conflicting versions and merge changes in whatever way makes sense for your document structure. If you can resolve it in code, you'll probably take some of the data from one of the conflict versions and copy it into the current version, until the current version contains the final merged version.

Once you've done that, save the document, remove the conflicting versions (removeOtherVersionsOfItemAtURL:error:), and mark the current file version as resolved. You might also want to use removeAndReturnError: to actually get rid of the older version(s).

Some more detail on this can be found in Apple's guide for resolving document version conflicts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top