I'm trying to call the method saveToURL forSaveOperation completionHandler on a UIManagedDocument however the completion block is not executing. It fails at (1) on the first attempt to save and fails at (2) at attempts after that. I'm not sure if this means it's writing to disk the first time or not. However, NSLogs in the completionHandler are never logged at all.
- (void)useDocument
{
CoreDataSingleton *cds = [CoreDataSingleton getInstance];
UIManagedDocument *document = cds.document;
NSURL *url = document.fileURL;
if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
NSLog(@"This is logged");
[document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"This is never logged");
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
// (1) Fails here on the first go
} else if (document.documentState == UIDocumentStateClosed) {
NSLog(@"This is logged");
[document openWithCompletionHandler:^(BOOL success) {
NSLog(@"This is never logged");
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
// (2) Fails here on the second go
} else {
self.managedObjectContext = document.managedObjectContext;
}
}
The class that implements this is a UIViewController which is a UICollectionView's data source and delegate. When I unhook the UIViewController as the data source the completionHandler is successful and the managedObjectContext is set.
Does anyone know what the issue would be?