I have a UIDocument
based app that uses NSFileWrapper
s to store data. The 'master' file wrapper contains many additional directory file wrappers, each of which represents a different page of the document.
Whenever I make a change to the document while the UIDocument
is saving (in writeContents:andAttributes:safelyToURL:forSaveOperation:error:
), the app crashes. Here is the stack trace:
It seems clear that I am modifying the same instance of file wrapper that the UIDocument
is enumerating over in the background. Indeed, I checked that when returning a snapshot of the data model in contentsForType:error:
, the returned sub file wrappers point to the same objects as the ones currently residing (and being edited) in the data model, and not copies.
- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
if (!_fileWrapper) {
[self setupEmptyDocument];
}
return [[NSFileWrapper alloc] initDirectoryWithFileWrappers:[_fileWrapper fileWrappers]];
}
This is the sanctioned approach to implementing this method (according to WWDC 2012 Session 218 - Using iCloud with UIDocument).
So I suppose the question is: How can this approach be thread safe?
Is the situation somehow different when the master file wrapper's fileWrappers
are themselves directory file wrappers? If the sanctioned approach is wrong, how should it be done?