【问题标题】:How to add more attributes to existing core data entity?如何为现有的核心数据实体添加更多属性?
【发布时间】:2013-04-12 12:26:28
【问题描述】:

我有一个使用核心数据的项目,我需要向现有实体(列)添加更多属性(列),如果我手动将属性添加到数据模型应用程序崩溃并且这是由于我曾经插入的上下文保存数据到表中

请帮忙..谢谢

【问题讨论】:

    标签: iphone ios core-data


    【解决方案1】:

    如果您只是向实体添加属性,则可以使用 Core Data 中的自动 lightweight migration

    基本上您所要做的只是在添加持久存储时通过适当的选项传递NSDictionary实例。这是_persistentStoreCoordinator的访问器方法末尾的代码sn-p:

    NSNumber *optionYes = [NSNumber numberWithBool:YES];
    NSDictionary *options = [NSDictionary dictionaryWithObjects:@[optionYes] forKeys:@[NSMigratePersistentStoresAutomaticallyOption]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Error opening persistent store: %@, %@", error, [error userInfo]);
        abort();
    }
    return _persistentStoreCoordinator;
    

    如果您的迁移对于轻量级迁移来说过于复杂,您会看到一个错误。否则迁移应该运行并且您的数据库将被更新以匹配您的新架构。

    请注意,如果您在设备上实际执行此操作,则应先备份 .sqlite 文件,以防迁移过程中出现问题。

    【讨论】:

      【解决方案2】:

      所以我的问题是我不知道这个持久存储协调器代码的去向。事实证明,当您在创建项目时选中“使用核心数据”时,它会在您的 AppDelegate 实现中自动创建。

      因此,从第二个链接here 开始,您需要做的所有轻量级迁移(添加新属性等)如下:

      1. 选择您的 .xcdatamodeld
      2. 从菜单中选择编辑器 -> 添加模型版本
      3. 随意命名新版本,在“基于模型”中选择以前的版本
      4. 在 .xcdatamodeld 的文件检查器中,选择模型版本 -> 当前 -> 您的新模型版本
      5. 在 Project Navigator 的 .xcdatamodeld 中选择新模型版本,然后对模型进行更改
      6. 如果您更改了属性名称或类型,请创建映射模型、新文件 -> 核心数据 -> 映射模型 -> 选择源和目标模型版本
      7. 更新新映射模型中的映射

      如下更改您的 AppDelegate 持久存储协调器代码。

      lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
        var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("<data model name>.sqlite")
        var error: NSError? = nil
        var failureReason = "There was an error creating or loading the application's saved data."
        let options = [
          NSMigratePersistentStoresAutomaticallyOption: true,
          NSInferMappingModelAutomaticallyOption: true]
        if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error) == nil {
            coordinator = nil
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason
            dict[NSUnderlyingErrorKey] = error
            error = NSError(domain: "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 coordinator
      }()
      

      因此,您只需在 addPersistentStoreWithType 调用中添加迁移选项。

      【讨论】:

      • 欣赏此答案中列出整个过程的额外步骤。如果已经有一段时间了,很容易忘记...
      猜你喜欢
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多