Question

When a UIDocument is closed, can it close again? Even when it's documentState is UIDocumentStateNormal after reopened?

Please help me. Thanks a lot!

[document openWithCompletionHandler:^(BOOL success) {
    if (success) {

        [document closeWithCompletionHandler:^(BOOL success) {
            if (success) {

                [document openWithCompletionHandler:^(BOOL success) {
                    if (success) {

                        NSLog(@"%d", document.documentState);

                        //Exception
                        [document closeWithCompletionHandler:^(BOOL success) {
                            if (success) {
                            } 
                        }];
                    } 
                }];
            } 
        }];
    } 
}];

When closeWithCompletionHandler called at the "Exception" mark, an exception is thrown:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'closeWithCompletionHandler called while document is already closing'
Was it helpful?

Solution

Notice that you are still inside the first closeWithCompletionHandler method when you call the second. By reading the exception that seems to be the cause.

OTHER TIPS

It seems you can only close a UIManagedDocument once per instatiation.

I had this problem, and solved it by, instead of re-opening the same document, I created a new one with the same data (using a custom initWith... method).

Then you can "re-close" the new instance of the document.

Keep in mind, the new instance of the document will have a new managed object context, so you probably should not try to use managed objects from the original document. Re-fetch them in the new instance if needed.

I have the same issue, and removing the second close solved it for me. I find as long as I use a separate UIManagedDocument instance for the second open/close, I don't get this error.

Also, I found I did not get the error consistently in previous code.

Opening and closing documents is an Async activity (meaning it does it on another thread in the background). By doing the opens and closes so close to one another you are overstepping the other process. Make sure the other thread is finished before you do the next step (an open or a close).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top