UIDocument openWithCompletionHandler:崩溃

我在我的项目中使用核心数据,并在下面的代码部分遇到罕见的崩溃

-(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) 

任何人都可以帮我解决这个问题? 它很less发生,但仍然非常不愉快

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

由于openWithCompletionHandler:asynchronous打开文档,当再次调用该方法时文档可能仍然打开。

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

如果你有一个exception断点,你可能会在控制台中看到类似下面的内容:

  *** 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: 

那么,UIDocument代码中的断言是失败的。 你可能应该提供更多的代码,因为你很明显的设置不当。

然后断言抛出一个exception。 如果在此方法中@catchexception,则可以loggingexception。

或者,您可以将自己的NSAssertionHandler分配给该线程,并直接查看断言。