迁移核心数据以添加新属性

时间:2015-08-06 09:40:02

标签: core-data core-data-migration

我添加了新属性,我需要迁移核心数据。结果,我喜欢这样。

Bus.xcDataModel>>添加模型版本

然后,我添加新属性。我也改变了持久存储协调器选项。

@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

但是当我添加新属性时,它会向我显示这样的内容。我该怎么办?

- [BusService setBus_wap:]:无法识别的选择器发送到实例0x1742a7320

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator_busservice
{
    if (_persistentStoreCoordinator_busservice != nil)
        return _persistentStoreCoordinator_busservice;

    NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"Busservice_new.sqlite"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]] &&
    !self.preloadEnabled)
    {
        NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Busservice_new" ofType:@"sqlite"]];

        NSError* err = nil;

    if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])
        DLog(@"Oops, could not copy preloaded data");
}

    NSError *error = nil;
_persistentStoreCoordinator_busservice = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator_busservice addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error])
{
    DLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator_busservice;
}

1 个答案:

答案 0 :(得分:1)

从该错误看起来,您在Core Data中向BusService实体添加了新属性,但未将该属性添加到BusService类。如果要对此新属性使用访问器方法,则还需要更新该类。或者您可以单独离开课程,但使用setValue:forKey:为新属性指定值。