Justin Paulson
Justin Paulson

Reputation: 4388

UIDocument openWithCompletionHandler not completing on iOS device

I am trying to open a managed document using openWithCompletionHandler:

The problem I am coming across is that it works fine on the simulator, but when I test it on my iPhone 4 the completion handler never finishes. The code looks like this:

[theManagedDocument openWithCompletionHandler:^(BOOL success){
    if(success) [self documentIsReady];
    if(!success) NSLog(@"Couldn't Open Document");
}];

This works fine on the simulator and I get to the documentIsReady call (or the "Couldn't Open Document" if it errors). But on the iPhone 4 it never runs the CompletionHandler block. I've put breakpoints all through the block (before and after both if statements) and nothing is getting called. No "Couldn't Open Document" on the console, no call to documentIsReady.

I must also mention that it seems like the first time I run the app on the iPhone it will work properly. I also have this encapsulated in an if statement with a fileExistsAtPath: call. It is getting inside the if statement just fine and calling the openWithCompletionHandler:, but the completion block just never gets fired.

I am using iOS 5.1 and Xcode 4.3.2.

Upvotes: 11

Views: 3438

Answers (5)

Mark Mc
Mark Mc

Reputation: 81

For me this was because I was calling openWithCompletionHandler: on a background thread. Changing it to be called from the main thread fixed the hang

dispatch_async(dispatch_get_main_queue(), ^{
    ...
    [document openWithCompletionHandler:^(BOOL success) {
        ...
    }];
});        

Upvotes: 0

damian
damian

Reputation: 4044

Another reason of why success might be false is that you changed your model (added a property to an entity for example) but didn't delete the app from simulator/device in order to get update the model.

Upvotes: 0

murat
murat

Reputation: 4963

I have experienced the same issue in my apps that use iCloud to share data. I realized that openWithCompletionHandler: just waits and never calls the completion handler if the document is left open by a suspended app. If that is the case for you, you should make sure that you are closing the document when your app goes to background.

Upvotes: 8

Dave Ross
Dave Ross

Reputation: 703

In my case, powering off my iPad and restarting it "fixed" this problem. Good luck!

Upvotes: 7

Nick Lockwood
Nick Lockwood

Reputation: 40995

Try checking that theManagedDocument != nil before the call - that's the only reason I can think of why the block would not be executed.

Upvotes: 2

Related Questions