0
votes

I am using Justin Driscoll's article on Core Data with UIManagedDocument in singleton pattern to set it up for UITabViewController. I am running the app on Simulator. Its working fine for the first time. The database is created successfully and I can see data in the tableview controller for each tab. But when I restart my application, app crashes with error

    Assertion failure in -[UIManagedDocument openWithCompletionHandler:],
     ** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
    reason: 'attempt to open or a revert document that already has an open or revert 
    operation in flight

My code that causes crash. My did some debugging using NSLog statements.

   if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) {
        NSLog(@"document doesnot exist and hence START CREATING");
        [self dataIntoDocument:self.document];
        NSLog(@"document Finished Creating");
        [self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:OnDocumentDidLoad];
        NSLog(@"Saved to URL on disk");
} else if (self.document.documentState == UIDocumentStateClosed) {
        NSLog(@"document is closed and its needs to be opened");
        [self.document openWithCompletionHandler:OnDocumentDidLoad];
} else if (self.document.documentState == UIDocumentStateNormal) {
        NSLog(@"test document is in normal state");
        OnDocumentDidLoad(YES);
}

RESULT FROM FIRST RUN When the database doesn't exists

 2013-08-12 14:51:35.458 <My APP>[368:11603] document doesnot exist and hence its created Start
2013-08-12 14:51:38.716 <My APP>[368:11603] document doesnot exist and hence its Finished Creating
2013-08-12 14:51:38.718 <My APP>[368:11603] NSManagedContext did save.
2013-08-12 14:51:38.718 <My APP>[368:11603] Saved to URL on disk
2013-08-12 14:51:38.721 <My APP>[368:11603] document is closed and its needs to be opened
2013-08-12 14:51:38.772 <My APP>[368:11603] NSManagedObjects did change

RESULT FROM SECOND RUN: When the database exists at the URL

2013-08-12 14:53:43.758 <MY APP> [380:11603] document is closed and its needs to be opened
2013-08-12 14:53:43.761 <MY APP>[380:11603] document is closed and its needs to be opened
2013-08-12 14:53:43.762 

I understand the reason why its failing, as its not supposed to open in close succession. I have same code in all view controllers which control the tabs to get instance of the UIManagedDocument. Please let me know what am I missing. Thanks

 -(id) initWithCoder:(NSCoder *)aDecoder {

          self = [super initWithCoder:aDecoder];
          if (!self.databaseDocument) {
              [[LTDatabaseDocumentHandler sharedDatabaseDocumentHandler] performWithDocument:^    (UIManagedDocument *document) {
              self.databaseDocument = document;
        [self populateTableViewArrayFromDocument:self.databaseDocument];
         }];
   }
   return self;
 }
2

2 Answers

0
votes

I figured. I need to use -(void)viewWillAppear:animated method to instantiate the shared instance of UIManagedDocument instead of -(id)initWithCoder. As the viewWillAppear method gets executed only when the user tries to view the tab.

0
votes

I don't understand how this helps you, but at least you figured it out. It just seems like viewWillAppear: may not be the best place to be initializing this kind of stuff.