Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Workaround for buggy UIDocument methods
@implementation UIDocumentSubclass
{
UIDocumentState myDocumentState;
}
- (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler
{
dispatch_queue_t originalQueue = dispatch_get_current_queue();
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
void (^fileAccessBlock)(void) = ^
{
__block NSError *error;
void (^coordinatedBlock)(NSURL *) = ^(NSURL *newURL)
{
BOOL success = [self readFromURL:newURL error:&error];
if (!success)
[self handleError:error userInteractionPermitted:YES];
dispatch_block_t completionBlock = ^
{
[self willChangeValueForKey:@"documentState"];
myDocumentState = (success ? UIDocumentStateNormal : UIDocumentStateClosed);
[self didChangeValueForKey:@"documentState"];
if (completionHandler)
completionHandler(success);
};
dispatch_async(originalQueue, completionBlock);
};
[coordinator coordinateReadingItemAtURL:self.fileURL options:0 error:&error byAccessor:coordinatedBlock];
};
[self performAsynchronousFileAccessUsingBlock:fileAccessBlock];
}
- (void)closeWithCompletionHandler:(void (^)(BOOL))completionHandler
{
dispatch_block_t block = ^
{
[self willChangeValueForKey:@"documentState"];
myDocumentState = UIDocumentStateClosed;
[self didChangeValueForKey:@"documentState"];
if (completionHandler)
completionHandler(YES);
};
dispatch_async(dispatch_get_current_queue(), block);
}
- (UIDocumentState)documentState
{
return myDocumentState;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment