Documentation of closeWithCompletionHandler"
says: "After the save operation concludes, the code in completionHandler is executed." However, in my app this code:
NSLog(@"closeWithCompletionHandler");
[self.document closeWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"completionHandler");
...
executes immediately (on iOS6.1):
2013-07-18 19:43:12.673 FooBar[819:907] closeWithCompletionHandler
2013-07-18 19:43:12.675 FooBar[819:907] completionHandler <-- look here
2013-07-18 19:43:16.234 FooBar[819:907] encoded
even though actual writing data to a file takes several seconds (I know that by tracking contentsForType:error:
).
contentsForType:error:
implementation looks like this:
- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
NSMutableDictionary* wrappers = [NSMutableDictionary dictionary];
[self encodeObject:self.data toWrappers:wrappers preferredFilename:kDocumentDataPath];
NSFileWrapper* fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];
NSLog("encoded");
return fileWrapper;
}
Please note encoding finishing long after completionHandler executes:
2013-07-18 19:43:12.675 FooBar[819:907] completionHandler
2013-07-18 19:43:16.234 FooBar[819:907] encoded <-- look here
Why is this so? How should I make sure that data is written out to file before proceeding?