0

我在这里建议使用 uiManagedDocument 单例:http: //adevelopingstory.com/blog/2012/03/core-data-with-a-single-shared-uimanageddocument.html

该文档由 fetchedResultsController 访问并显示在 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]]];
      }

}

//在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];
        }
}

该应用程序在 6.0 模拟器上运行良好,但在设备上,表格是空白的,并且文档显示一个问题,即 UIDocumentStateSavingError 或 UIDocumentStateEditingDisabled。我不知道是什么导致它在手机和模拟器上的运行方式不同

4

0 回答 0