I would like to implement asynchronous saving in my NSDocument. According to the documentation, I need to return YES in canAsynchronouslyWriteToURL:ofType:forSaveOperation:
and then call unblockUserInteraction
sometime during writing.
In my document, writing is performed by fileWrapperOfType:error:
by simply returning a NSFileWrapper.
- (NSFileWrapper*) fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
return self.myMutableFileWrapper;
}
About this, the documentation says:
For example, the default implementation of fileWrapperOfType:error: invokes [unblockUserInteraction] when it has created the NSFileWrapper object to return. Assuming that the NSFileWrapper is not mutated by subsequent user actions, it is effectively a "snapshot" of the document's contents, and once it is created it is safe to resume handling user events on the main thread, even though some of those user events might change the document's contents before the NSFileWrapper object has been safely written.
Currently in my code self.myMutableFileWrapper
can be changed after fileWrapperOfType:error
, so I can't assume it won't be mutated like the documentation says.
Does this mean that I should modify fileWrapperOfType:error:
to make a copy of my file wrapper, and then call unblockUserInteraction
? Something like this:
- (NSFileWrapper*) fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
NSFileWrapper *copyFileWrapper = MakeACopyOf(self.myMutableFileWrapper);
[self unblockUserInteraction];
return copyFileWrapper;
}
If the above is correct, how do I make a copy of a NSFileWrapper? Also, doesn't making a copy of a potentially huge NSFileWrapper negate the performance benefits of asynchronous saving?