Question

I have an iOS app that implements a custom subclass of UIDocument that encapsulates a file wrapper on a package of data, archived objects and images. I initially converted the implementation to UIDocument for iCloud support but it has been too unreliable and so I am pulling out the iCloud portion and just using a UIDocument with local storage.

When the user modifies the data I send updateChangeCount: to the UIDocument instance and it periodically autosaves in the background. I only close the document when the application goes to the background. I've discovered that after an autosave which calls my contentsForType method of UIDocument a loadFromContents is also called. I don't understand this logically and it is also causing problems with my interface.

I thought loadFromContents would only be called when opening the document or if an iCloud document was modified on another device. So when my document autosaves in the background it also re-loads which causes an update of the UI which can sometimes discard new work in progress that hasn't been saved. I could ignore the re-loading in the UI but I'd still have some problems with my model data being different than the UI objects.

So what am I doing wrong to cause this behavior? I would like to have a save not initiate a loadFromContents or understand how to handle that behavior.

Thanks!

Was it helpful?

Solution

I discovered that this behavior was somehow due to some @synchronized calls I had wrapped contentsForType and the writeContents: methods with on initial implementation to prevent crashes when closing the document with unsaved changes. I was getting two near simultaneous calls to contentsForType that caused bad access errors.

I found a cleaner way to prevent these crashes by setting updateChangeCount:UIDocumentChangeCleared before closing the document.

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