UIDocument openWithCompletionHandler: crash

不打扰是莪最后的温柔 提交于 2019-11-28 05:02:07

问题


I'm using Core Data in my project and get a rare crash in the following code section

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

This is what crash log says:

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)

Can anyone help me to solve this issue? It happens very seldom, but still is very unpleasant


回答1:


The error occurs if your app attempts to call your useDocument method twice in close succession.

Because the openWithCompletionHandler: opens the document asynchronously, the document may still be opening when the method is called again.

If this happens, your app ends up trying to open the document twice (as the document state will remain UIDocumentStateClosed until completion) and this causes the exception to be thrown.

If you have an exception breakpoint, you may see something like this in the console:

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



回答2:


Well, an assertion is failing in the UIDocument code. You should probably provide more code, because you are obviously setting something up improperly.

The assertion is then throwing an exception. If you @catch exceptions in this method, you can log the exception.

Or, you can assign your own NSAssertionHandler to the thread, and see the assertion directly.



来源:https://stackoverflow.com/questions/11933055/uidocument-openwithcompletionhandler-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!