我一直在尝试在我组装的应用程序中使用自定义迁移策略。到目前为止,当使用Map模型时,迁移可以从v1-〉v2工作。然而,每当我向实体Map添加自定义策略时,迁移拒绝从v2-〉v3工作。
自定义迁移策略:
import Foundation
import CoreData
class ObjectCustomV2V3Migration: NSEntityMigrationPolicy {
override func createDestinationInstancesForSourceInstance(sInstance: NSManagedObject, entityMapping mapping: NSEntityMapping, manager: NSMigrationManager, error: NSErrorPointer) -> Bool {
var newObject:NSManagedObject? = NSEntityDescription.insertNewObjectForEntityForName(mapping.destinationEntityName!, inManagedObjectContext: manager.destinationContext) as? NSManagedObject
// Sets attr31 - string attribute
var str:String = sInstance.valueForKey("attr21") as String
if str == "" {
str = "BlanketyBlank"
}
newObject?.setValue(str, forKey: "attr31")
// Sets attr32 (int16 attribute) as double of value in previous version
// ignore the dodgy type casting.
var num:Int = sInstance.valueForKey("attr22") as Int
num *= 2
var num16 = NSNumber(integer: num)
newObject?.setValue(num16, forKey: "attr32")
if newObject != nil {
manager.associateSourceInstance(sInstance, withDestinationInstance: newObject!, forEntityMapping: mapping)
return true
}
return false
}
}
当我运行应用程序时,返回以下错误:
2015-07-10 14:32:42.952 SingleDBMigration[2153:739674] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/6C142EC2-02DB-4BD6-8428-5739C57C7795/Documents/SingleDBMigration.sqlite options:{
NSInferMappingModelAutomaticallyOption = 0;
NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134110 "The operation couldn’t be completed. (Cocoa error 134110.)" UserInfo=0x17ecad70 {NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)} with userInfo dictionary {
NSUnderlyingException = "Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)";
}
2015-07-10 14:32:42.965 SingleDBMigration[2153:739674] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x17eaf880 {NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x17ecad90 "The operation couldn’t be completed. (Cocoa error 134110.)", NSLocalizedFailureReason=There was an error creating or loading the application's saved data.}), Optional([NSLocalizedDescription: Failed to initialize the application's saved data, NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134110 "The operation couldn’t be completed. (Cocoa error 134110.)" UserInfo=0x17ecad70 {NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)}, NSLocalizedFailureReason: There was an error creating or loading the application's saved data.])
其中重要的部分是:
NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)
我已经试过了我发现的关于这个问题的几个问题,没有一个能提供任何满意的解决方案。如果有人能给我所面临的问题提供任何帮助,我将非常感谢!
谢谢!
3条答案
按热度按时间iovurdzv1#
在理解了一个类似问题的答案之后,我最终解决了这个问题,我忘记了添加项目名称空间,所以我应该使用
ProjectModuleName.ObjectCustomV2V3Migration
,而不是在Mapping Model中将自定义策略名称设置为ObjectCustomV2V3Migration
。anauzrmj2#
我的问题是我的项目名称中有空格,所以在Map模型编辑器中,当指定Swift模块名称时,应该使用下划线而不是空格来指定定制策略。
例如,为了在名为My App Name的项目中指定
MyCustomMigrationPolicy
类,我在编辑器中使用了以下自定义策略:My_App_Name.MyCustomMigrationPolicy
kmpatx3s3#
在Map模型中添加模块名作为前缀是解决这个问题的一种方法,但是在多目标项目中,您必须为每个目标创建一个Map模型。
另一方面,您可以使用
@objc(name)
属性标记自定义策略类,并将类名暴露给Objective-C运行时,前面不带模块名,如下所示:这样,就不需要在Map模型中用模块名作为类名的前缀: