I'm trying to open a UIManagedDocument
after creating it. It seems to be unable to open the document after a few times of successfully opening the document. If I change the document name or delete/copy the app again, it works. The below method is called in the app delegate every time the application launches.
-(void) loadDataDocument {
NSURL *documentURL= [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"TestDataDocument1"];
self.document = [[UIManagedDocument alloc] initWithFileURL:documentURL];
if(![[NSFileManager defaultManager] fileExistsAtPath:[documentURL path]]) {
[self.document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
[self documentIsReady];
}else {
NSLog(@"Tried to create new file. Cannot open document");
}
}];
} else if (self.document.documentState == UIDocumentStateClosed) {
[self.document openWithCompletionHandler:^(BOOL success) {
if (success) {
[self documentIsReady];
}else {
NSLog(@"Document was Closed. Cannot open document");
}
}];
} else {
[self documentIsReady];
}
}