我有一个关于上下文派生自UIManagedDocument的CoreData的问题。
在下面的代码片段中,它从不记录“打开文档时出错”,而总是记录“文档仍然关闭”--为什么我不能打开文档?有什么想法吗?
-(void)openDocument
{
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Date Database"];
UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]])
{
[document openWithCompletionHandler:^(BOOL success){
if (!success) {
// Handle the error.
NSLog(@"Error opening the document");
}
}];
}
else
{
[document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
if (!success) {
// Handle the error.
NSLog(@"Error saving the file");
}
}];
}
self.theDocument = document;
if (self.theDocument.documentState == UIDocumentStateClosed)
{
NSLog(@"Document still closed!");
}
}发布于 2012-08-23 06:32:18
openWithCompletionHandler是一种异步方法。它只启动一个后台线程来打开和读取文档。当您检查documentState时,这个后台线程可能还没有结束,因此状态仍然是“已关闭”。
打开文档(或文档失败)后,openWithCompletionHandler将执行completionHandler块。
https://stackoverflow.com/questions/12075301
复制相似问题