3

I am running into a weird problem. I am sure that I did something to a file somewhere else in my code and it didn't close properly or something, but now it is in a state where it reports as being closed, but when I call OpenWithCompletionHandler it never returns. See below:

   //if the file is closed, open it and then set up the controller
    if (file.documentState == UIDocumentStateClosed){
        //---- this code executes        
        [file openWithCompletionHandler:^(BOOL success){
           // ---- this code NEVER executes
        }];
    }

Any ideas?

Drew Rosenberg
  • 899
  • 6
  • 7
  • Did you ever figure out what was the problem? After I upgraded to the 6.0 SDK I can't get openWithCompletionHandler to work in iOS 5.1. It never returns. – Breno Gazzola Sep 23 '12 at 20:56
  • Nope. I have not seen the problem in a couple of weeks, but I am also now on iOS6. – Drew Rosenberg Sep 28 '12 at 05:34
  • I'm having the same issue... using XCode 4.5.2 (4G2008a), using iOS Simulator 6.0 but with iOS 5.1 OS... openWithCompletionHandler never fires on a closed database. I have a HUD which lowers on the completion firing thus it is painfully obviously in the simulator since the HUD stays up. – Joe Jan 13 '13 at 20:39

2 Answers2

0

See Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument.

My solution was along the same lines as those reported, but I had to lower the Deployment Target of my app to iOS 5.0 so that "iPhone 5.0 Simulator" was available as a run target. I have only seen this issue trying to use the iPhone 5.1 Simulator with XCode 4.5.2, both the 5.0 and 6.0 simulators work.

Community
  • 1
  • 1
Joe
  • 2,352
  • 20
  • 38
0

I was having the same problem.

Are you trying to open the document inside viewDidLoad?

Try moving the code to another method. It solved the problem for me.

in ViewController.h

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong) UIManagedDocument *document;

in ViewController.m

@synthesize managedObjectContext = _managedObjectContext;
@synthesize document = _document;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do not try to open the document here
    // Call another method instead :D
    if (!_managedObjectContext) {
        [self createContext];
    }
}

- (void)createContext
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *url = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"Database"];

    self.document = [[UIManagedDocument alloc] initWithFileURL:url];

    // FILE DOES NOT EXIST - Let's create a new one    
    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        [self.document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"ERROR: Cannot create new document");
            }
        }];

    // FILE IS CLOSED - Let's open it
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                self.managedObjectContext = self.document.managedObjectContext;
            } else {
                NSLog(@"File is closed and it wont open!");
            }
        }];

    // FILE EXISTS AND IS OPENED - Yay!
    } else {
        self.managedObjectContext = self.document.managedObjectContext;
    }
}
GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57