【问题标题】:Error adding NSEntity to a NSOrderedSet将 NSEntity 添加到 NSOrderedSet 时出错
【发布时间】:2015-12-08 08:58:26
【问题描述】:

我的 CoreData 结构中有 2 个实体,它们之间存在一对多的关系。在主实体中,我有一个 NSOrderdSet,每次进行新保存时,我都想在 NSOrderdSet 中添加并保存第二个实体,但出现错误。 这是我的代码:

//main Entity 
class Project : NSManagedObject {
func requestAddNewWeek (value: WeekContent) {
let mutableCopyOfSet = projectContent?.mutableCopy() as! NSMutableOrderdSet
mutableCopyOfSet.addObject(value)
projectContent = mutableCopyOfSet.copy() as ? NSOderedSet
}
}

// ViewController where the save occurs
class SetupWeekViewController {
var project : Project?

@IBAction func saveButton (sender : AnyObject) {
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext
 let entity = NSEntityDescription.entityForName("Content", inManagedObjectContext:managedObjectContext
//Here is where the error happens
project!requestAddNewWeek(newWeek)

managedContext.save()
}

//这是我得到的错误

2015-12-08 11:07:13.392 MySightwords[1998:189666] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/Len/Library/Developer/CoreSimulator/Devices/0215DFE7-14EA-459F-991C-5ABF8020712D/data/Containers/Data/Application/7A509174-D568-4CCE-8A3D-9710FF80A9BF/Documents/SingleViewCoreData.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_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 = 640;
        NSStoreModelVersionHashes =         {
            Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
            Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
            Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
2015-12-08 11:07:13.396 MySightwords[1998:189666] Unresolved error Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7ff7025e9200 {Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}}}, [NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 640;
    NSStoreModelVersionHashes =     {
        Content = <16a6b5d3 e57d053c f85d1afa 2efe7c04 8da974e7 127f2c6b 847ea2f0 b63d732f>;
        Project = <97588fff 696dabc6 896ec617 564b473f f3bc9bdc e6c891b3 84519149 9319c454>;
        Sightwords = <6bf96219 adc3369d 4eec0597 b40472c3 d3af6a61 3d17bd5d 117885f4 509ecd2e>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "270A2DB1-BFDA-41A3-B044-DFBA717A1BDD";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}]
(lldb) 

【问题讨论】:

  • 你能告诉我错误吗
  • 好的,我添加了我得到的错误,它有点长
  • 您是否在模拟器中运行应用程序?
  • 尝试删除您的应用并重新安装,当您更改核心数据模型时会发生这种情况。

标签: ios core-data nsmanagedobject swift2.1


【解决方案1】:

这是由于您的模型发生了变化。

从模拟器中移除应用并对您的项目执行清理。

注意:这仅用于开发。对于生产,您需要实施某种迁移。谷歌“核心数据迁移”,轻量级迁移是最简单的。

【讨论】:

  • 清除我的模拟器后,应用程序崩溃了,因为它打开了一个零。 project!requestAddNewWeek(newWeek) 解包为 nil。
  • 我认为数据迁移可能会起作用,因为我更改了实体的名称,所以商店和模型不匹配,所以项目模型将展开为 nil
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多