UIDocument openWithCompletionHandler not completing on iOS device
Asked Answered
A

5

11

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.

Autocephalous answered 12/4, 2012 at 15:20 Comment(0)
S
7

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

Snack answered 24/4, 2012 at 21:37 Comment(2)
I cleared out the app, restarted everything and it seems to be doing better. A little more work than this answer, but this is close enough. Still not sure what was causing it.Autocephalous
It's eerie how often restarting your device fixes bugs.Zucker
C
8

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.

Cloakroom answered 25/5, 2012 at 21:42 Comment(2)
I have also experienced essentially the same behavior if I forget to release my custom UIDocument class instance from within the completion handler. It just stays open and will hang the second time I try to call openWithCompletionHandler.Haematothermal
@Haematothermal That would make sense... except that this problem also occurs with ARC (which handles memory).Heptarchy
S
7

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

Snack answered 24/4, 2012 at 21:37 Comment(2)
I cleared out the app, restarted everything and it seems to be doing better. A little more work than this answer, but this is close enough. Still not sure what was causing it.Autocephalous
It's eerie how often restarting your device fixes bugs.Zucker
P
2

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

Paramnesia answered 16/4, 2012 at 12:38 Comment(0)
J
0

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.

Jerid answered 25/2, 2013 at 16:17 Comment(1)
possibly, but I wasn't getting success as false. I wasn't getting anything returned at all.Autocephalous
A
0

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) {
        ...
    }];
});        
Abuttals answered 14/8, 2020 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.