UIManagedDocument OpenWithCompletionHandler永远不会返回

时间:2012-09-15 17:23:17

标签: ios uimanageddocument

我遇到了一个奇怪的问题。我确信我在我的代码中的某个地方对某个文件执行了某些操作并且它没有正确关闭或者某些东西,但现在它处于报告为关闭的状态,但是当我调用OpenWithCompletionHandler时它永远不会返回。见下文:

   //if the file is closed, open it and then set up the controller
    if (file.documentState == UIDocumentStateClosed){
        //---- this code executes        
        [file openWithCompletionHandler:^(BOOL success){
           // ---- this code NEVER executes
        }];
    }

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

请参阅Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument

我的解决方案与报告的解决方案相同,但我不得不将应用程序的部署目标降低到iOS 5.0,以便“iPhone 5.0 Simulator”可用作运行目标。我只看到了这个问题,试图将iPhone 5.1模拟器与XCode 4.5.2一起使用,5.0和6.0模拟器都可以工作。

答案 1 :(得分:0)

我遇到了同样的问题。

您是否尝试在viewDidLoad中打开文档?

尝试将代码移动到另一种方法。它解决了我的问题。

ViewController.h中的

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong) UIManagedDocument *document;
ViewController.m中的

@synthesize managedObjectContext = _managedObjectContext;
@synthesize document = _document;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do not try to open the document here
    // Call another method instead :D
    if (!_managedObjectContext) {
        [self createContext];
    }
}

- (void)createContext
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *url = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"Database"];

    self.document = [[UIManagedDocument alloc] initWithFileURL:url];

    // FILE DOES NOT EXIST - Let's create a new one    
    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"ERROR: Cannot create new document");
            }
        }];

    // FILE IS CLOSED - Let's open it
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"File is closed and it wont open!");
            }
        }];

    // FILE EXISTS AND IS OPENED - Yay!
    } else {
        self.managedObjectContext = self.document.managedObjectContext;
    }
}