1

我有一个使用核心数据的应用程序。目前我使用的是 V1.5,在这些版本中,我对核心数据库进行了一些更改。在有人抱怨该应用程序仍然崩溃后,我发现我应该实施Core data lightweight。现在的问题是,我不再知道我在不同版本中所做的更改。

同样为了处理核心数据,我遵循了斯坦福大学的视频教程。 这里是第13课。

当您查看本教程时,您会发现他无法使用 XCODE 在 appdelegate 中生成的标准代码。我将简要解释他在做什么。

在 deViewWillAppear我检查是否有托管文档。如果没有,我创建一个。

  if (!self.genkDatabase) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Default appGenk Database"];
        // url is now "<Documents Directory>/Default Photo Database"
        self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
    }

设置文档后,我调用该UseDocument函数

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
}

然后在 useDocument 我检查文档状态是什么,然后获取并将我的数据保存在核心数据库中。

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
        // does not exist on disk, so create it
        [self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"no database created yet");
             [self fetchNewsIntoDocument:self.genkDatabase];
           [self setupFetchedResultsController];
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
        // exists on disk, but we need to open it
        NSLog(@"closed");
        [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
            
                [self fetchNewsIntoDocument:self.genkDatabase];
            [self setupFetchedResultsController];     
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
        // already open and ready to use
        [self setupFetchedResultsController];
    }
}

这就是我将数据提取到数据库中的方式。

- (void)fetchNewsIntoDocument:(UIManagedDocument *)document
{
    dispatch_queue_t fetchNews = dispatch_queue_create("news fetcher", NULL);
    dispatch_async(fetchNews, ^{
        NSArray *news           = [GenkData getNews];
     [document.managedObjectContext performBlock:^{
    
        int newsId          = 0;
        for (NSDictionary *genkInfo in news) {
            newsId++;
            [News newsWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withNewsId:newsId];
        }
     
        [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
       }];
    });
   dispatch_release(fetchNews);
}

我知道这是很多代码,但我想得到一个好的答案,因为我正在寻找这个问题好几天。在我听说轻量级核心数据迁移之前,我的 appdelegate 中没有任何关于核心数据的内容。在研究了轻量级迁移之后,我在我的 appdelegate 中添加了标准的 core-dat 代码,并将其添加到代码中。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Default appGenk Database.sqlite"];
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
    
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return _persistentStoreCoordinator;
}

现在经过大量代码,我们正在解决实际问题。当我在安装了第一个版本的 iPad 上运行项目时,在将数据插入数据库后出现以下错误。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

我希望任何人都可以帮助我解决这个问题。我不是 IOS 开发人员,我很难解决这个问题。

亲切的问候

4

0 回答 0