Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

ios - UIDocument openWithCompletionHandler: crash

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...