openWithCompletionHandler

openWithCompletionHandler

我在我的项目中使用 Core Data 并在以下代码部分遇到罕见的崩溃

 -(void) useDocument{
     AFFormsCoreDataEngine* engine = [AFFormsCoreDataEngine sharedInstance];
     if (![[NSFileManager defaultManager] fileExistsAtPath: [engine.formsDatabase.fileURL path]])
     {
         [engine.formsDatabase saveToURL: engine.formsDatabase.fileURL forSaveOperation: UIDocumentSaveForCreating completionHandler: ^(BOOL success){
            // setup
         }];
    }
    else if (engine.formsDatabase.documentState == UIDocumentStateClosed)
    {
        [engine.formsDatabase openWithCompletionHandler: ^(BOOL success){
             // setup
        }];
    }
    else if (engine.formsDatabase.documentState == UIDocumentStateNormal)
    {
         // setup
    }
}

这是崩溃日志所说的:
Last Exception Backtrace:
0   CoreFoundation                  0x371fd88f __exceptionPreprocess + 163
1   libobjc.A.dylib                 0x31272259 objc_exception_throw + 33
2   CoreFoundation                  0x371fd789 +[NSException raise:format:] + 1
3   Foundation                      0x32ce83a3 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 91
4   UIKit                           0x306b3149 -[UIDocument openWithCompletionHandler:] + 173
5   EETECH                          0x00014d23 -[AFFormListViewController useDocument] (AFFormListViewController.m:150)

谁能帮我解决这个问题?它很少发生,但仍然很不愉快

最佳答案

如果您的应用程序尝试连续两次调用您的 useDocument 方法,则会发生该错误。

因为 openWithCompletionHandler: 异步打开文档,再次调用该方法时,文档可能还在打开状态。

如果发生这种情况,您的应用程序最终会尝试打开文档两次(因为文档状态将保持 UIDocumentStateClosed 直到完成),这会导致抛出异常。

如果您有异常断点,您可能会在控制台中看到如下内容:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to open or a revert document that already has an open or revert operation in flight:

关于ios - UIDocument openWithCompletionHandler : crash,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11933055/

10-10 23:08