我试图NSManagedDocument
在创建它之后打开它。成功打开文件几次后似乎无法打开文件。如果我更改文档名称或再次删除/复制应用程序,它可以工作。每次应用程序启动时,都会在应用程序委托中调用以下方法。
-(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];
}
}