【发布时间】:2015-01-27 11:52:40
【问题描述】:
我正在 Core Data 中进行手动/大量迁移。我有数据模型,即myApp2.xcdatamodel 和myApp3.xcdatamodel。
myApp3 在两个表之间有一个新表(如联结表),它们都处于TO-MANY 关系中。
此外,我还定义了一个NSEntityMigrationPolicy。现在,当我使用此自定义策略(专门为关系和管理数据而编写)运行时,我在调用 migrationManager.migrateStoreFromURL
Optional("Error Domain=NSCocoaErrorDomain Code=134110 \"The operation couldn’t be completed. (Cocoa error 134110.)\" UserInfo=0x7feda0d825b0 {NSUnderlyingException=Couldn\'t create mapping policy for class named (MyTableToMyTablePolicy)}")
我试图查看此错误,但从某种方式来看,它以 Policy 类为目标,我已将其粘贴在下面。
class MyTableToMyTablePolicy: NSEntityMigrationPolicy {
override func createDestinationInstancesForSourceInstance(sourceInstance: NSManagedObject, entityMapping mapping: NSEntityMapping, manager: NSMigrationManager, error: NSErrorPointer) -> Bool {
let sourceKeys:[AnyObject] = sourceInstance.entity.attributesByName.keys.array
let sourceValues = sourceInstance.dictionaryWithValuesForKeys(sourceKeys)
let destinationInstance:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName(mapping.destinationEntityName!, inManagedObjectContext: manager.destinationContext) as NSManagedObject
let destinationKeys = destinationInstance.entity.attributesByName.keys.array as [AnyObject]
if mapping.userInfo!["modelVersion"]!.integerValue == 2 {
for key in destinationKeys {
let value:AnyObject = sourceValues[key as String]!
if value.hasText() {
destinationInstance.setValue(value, forKey: key as String)
}
}
}
else { // call the super just
super.createDestinationInstancesForSourceInstance(sourceInstance, entityMapping: mapping, manager: manager, error: error)
}
manager.associateSourceInstance(sourceInstance, withDestinationInstance: destinationInstance, forEntityMapping: mapping)
return true
}
}
以下分别是 xcdatamodels myApp2 和 myApp3:
更新:崩溃日志:
【问题讨论】:
-
它说它无法创建迁移策略。您是否需要在自定义迁移策略名称前加上 swift 模块?
-
@k6sandeep 非常好。
-
@Sandeep 这绝对是我的解决方案!