【问题标题】:Core Data error when fetching获取时核心数据错误
【发布时间】:2016-03-19 21:36:42
【问题描述】:

我是 Core Data 的新手,我一直在尝试获取我创建的实体,但我不断收到错误消息。

错误如下:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/4A0BBC17-674F-4A5D-80F7-72174D71B95F/Documents/Todogorithm.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Task = <3db44fe3 d21215ef eee9476a ae204647 b49829de 00b4784c 7715b5ff a252dcab>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "DFB8723E-7D5B-44A0-AFB9-E9E0F7E39E85";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
    metadata =     {
        NSPersistenceFrameworkVersion = 641;
        NSStoreModelVersionHashes =         {
            Task = <3db44fe3 d21215ef eee9476a ae204647 b49829de 00b4784c 7715b5ff a252dcab>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "DFB8723E-7D5B-44A0-AFB9-E9E0F7E39E85";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}

当调用我的视图控制器上的viewDidAppear 时会引发错误。这是我的viewDidAppear: [超级 viewDidAppear:动画];

AppDelegate *appDelegate = [[AppDelegate alloc] init];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Task"];
_results = [context executeFetchRequest:request error:nil];

那里的代码在我的appDelegate 中调用managedObjectContext,在该方法中调用persistentStoreCoordinator 方法(这是实际引发错误的地方)。 AppDelegate.m 中的两个方法如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Todogorithm.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}


- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];

关于为什么调用persistentStoreCoordinator 中的错误有任何想法吗?

【问题讨论】:

  • 我认为,您应该先尝试删除/卸载此应用程序,然后再运行它。

标签: ios objective-c sqlite core-data


【解决方案1】:

您可能在 Xcode 中更改了托管对象模型,但您仍然在设备/模拟器上安装了具有旧数据模型配置的应用程序。

这就是您收到此错误的原因。 如果您的应用是全新的,并且您不必担心与已安装应用的兼容性,您可以从设备/模拟器中删除旧应用并安装新应用。

如果您需要保持旧数据模型配置与新数据模型配置的兼容性,则应使用模型的版本控制并在较新的数据模型版本中进行所有更改。

【讨论】:

    【解决方案2】:

    从模拟器/设备中删除应用程序,执行清理并重新构建您的项目。

    每当您对核心数据定义进行更改时,删除安装在物理设备或模拟器上的应用程序,清理项目并重新构建。

    【讨论】:

      【解决方案3】:

      清除您的应用数据或从您的设备中删除该应用以解决此问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 2012-01-20
        相关资源
        最近更新 更多