Skip to content

Instantly share code, notes, and snippets.

@hatfinch
Created January 20, 2012 17:43
Show Gist options
  • Save hatfinch/1648636 to your computer and use it in GitHub Desktop.
Save hatfinch/1648636 to your computer and use it in GitHub Desktop.
// 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