用于打开商店的模型配置与用于创建商店的模型配置不兼容

Hun*_*ter 1 xcode core-data ios swift

嘿,我在线路上总是遇到这种错误的访问

try managedContext.save()
Run Code Online (Sandbox Code Playgroud)

代码完美运行,直到它尝试保存我的值。我认为这可能是因为我使用的是实体的“副本”而不是实际的实体。但这是必要的,因为您无法在代码中实际编辑原始模型。所以我所要做的就是在尝试“.save()”时停止这种错误的访问,我很好。它位于代码的最后 4 行。

所以基本上在使用时尝试 ManagedContext.save()。我创建了一个将打印错误的捕获。所以我把“!” “try”中的符号,以便我可以了解应用程序抛出错误的原因,这就是我得到的

致命错误:“尝试!” 表达式意外引发错误:错误域 = NSCocoaErrorDomain 代码 = 134020 “用于打开商店的模型配置与用于创建商店的模型配置不兼容。”

错误图片

代码:

let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let managedContext: NSManagedObjectContext =  appDelegate.managedObjectContext

var properties0 = Array<NSAttributeDescription>()

let playersList0 = NSEntityDescription.entityForName("PlayersList1", inManagedObjectContext: managedContext)

let copy = playersList0!.copy() as! NSEntityDescription

let contentTypeAttribute0 = NSAttributeDescription()
contentTypeAttribute0.name = "firstName"
contentTypeAttribute0.attributeValueClassName = "firstName"
contentTypeAttribute0.attributeType = .StringAttributeType
contentTypeAttribute0.optional = true
properties0.append(contentTypeAttribute0)


copy.properties = properties0

let playerslistCopyto = NSManagedObject(entity: copy, insertIntoManagedObjectContext: managedContext)

playerslistCopyto.setValue("John", forKey: "firstName")



do {
    try managedContext.save()
} catch {
    print("Error") //prints Error everytime
}
Run Code Online (Sandbox Code Playgroud)

每次都会打印“Error”作为捕获。我基本上只需要了解如何保存“副本”

rha*_*pso 5

抱歉,这是一条旧消息,但似乎没有很多有用的答案。我上周确实遇到了类似的错误:

Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store." UserInfo={problemObject=<CachedWeightedRegularJourney: 0x6000023c5130> (entity: CachedWeightedRegularJourney; id: 0x6000002605e0 <x-coredata:///CachedWeightedRegularJourney/t05BA08BA-E646-44D9-8B36-713C5B4499629>; data: {
    destinationURN = "urn:trainline:generic:loc:1183";
    originURN = "urn:trainline:generic:loc:4916";
    weight = 1;
})}
Run Code Online (Sandbox Code Playgroud)

我的案例的根本原因是,在我的模型中添加新实体之前,我没有看到我的模型包含多个配置。因此,仅添加实体而不将其添加到正确的模型配置中会导致保存失败。保存时似乎会发生协调器丢失的情况,因为它找不到将保存更改的持久存储。

我的案例的解决方案:我将新实体拖放到模型编辑器中的正确配置中。