0

I use a uiManagedDocument singleton as suggested here: http://adevelopingstory.com/blog/2012/03/core-data-with-a-single-shared-uimanageddocument.html

the document is accessed by a fetchedResultsController and displayed in a tableViewController.

+ (DocumentHandler *)sharedDocumentHandler
    {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

        url = [url URLByAppendingPathComponent:@"ElphiDB"];
        self.document = [[UIManagedDocument alloc] initWithFileURL:url];
        [self.logger log:@"DocumentHandler Initializing document"];
        // Set our document up for automatic migrations
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
        self.document.persistentStoreOptions = options;


     }
     return self;
}


- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{

    void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) { 
         onDocumentReady(self.document);
        [self.logger log:[NSString stringWithFormat:@"performWithDocument success=%d",success]];
        };



    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) {
        [self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForCreating     completionHandler:OnDocumentDidLoad];
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:OnDocumentDidLoad];

        NSLog(@"DocumentHandler performWithDocument Open:%@",[self.document description]);        
    } else if (self.document.documentState == UIDocumentStateNormal) {
        OnDocumentDidLoad(YES);
        NSLog(@"DocumentHandler performWithDocument state normal");
    }
   if (self.document.documentState != UIDocumentStateNormal) {
       [self.logger log:[NSString stringWithFormat: @"DocumentHandler problem-%@",[self.document description]]];
      }

}

//in tableViewController

 - (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated];
     RootNavigationController *rootNC=(RootNavigationController*)self.navigationController;
         if (!self.plugDocument) {
             [[DocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
                [self.logger log:[NSString stringWithFormat:@"PlugsTVC VWA - call documentHandler doc=%@",document]];
                self.plugDocument=document;
                self.plugFetcher.document=document;
                [self setupFetchedResultsController];
                [self fetchPlugDataIntoDocument:document];
            }];
         }else{
            [self.logger log:[NSString stringWithFormat:@"PlugsTVC VWA  doc=%@ does exist",self.plugDocument]];
            [self fetchPlugDataIntoDocument:self.plugDocument];
        }
}

The app works fine on the 6.0 simulator but on the device, the table is blank and the document shows a problem, either UIDocumentStateSavingError or UIDocumentStateEditingDisabled. I have no idea what could cause this to run differently on the phone vs. the simulator

mflac
  • 353
  • 3
  • 16
  • That's it, a UIDocumentStateSavingError? Usually there's an explanation of why, what's wrong, or at the very least an error number. One thing I can think of is the location of the document. NSLog `self.document.fileURL`. This cannot be in the main bundle, as devices can't write to the main bundle. The simulator would give you no problems though. – Scott Berrevoets Oct 10 '12 at 05:03
  • Just curious: which iOS version is the device running? – brainray Oct 10 '12 at 11:02
  • Another question: where do you call 'performWithDocument'? In viewWill appear? I am also working with this Singleton the last days (from that tutorial). – brainray Oct 10 '12 at 11:05
  • i added the init method to show that the document is being created in the documents sub-directory. In answer to Scott's question: the app doesn't crash, but when i check if (self.document.documentState != UIDocumentStateNormal) i get the above mentioned errors. – mflac Oct 10 '12 at 18:35
  • i added the init method to show that the document is being created in the documents sub-directory. In answer to Scott's question: the app doesn't crash, but when i check if (self.document.documentState != UIDocumentStateNormal) i get the above mentioned errors. i am using the iOS 6 simulator, where it works fine, but on a iphone4s with iOS 5.1 an d an iPhone 5 with iOS6. And yes i call performWithDocument in the tableViewControllers viewDidAppear. I've added code above to show this. – mflac Oct 10 '12 at 18:42

0 Answers0