Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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.

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) {
        ...
    }];
});        
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top