I've been trying to use a custom migration policy in an application I'm putting together. So far migrations have worked from v1 -> v2 when using a mapping model. However, whenever I add a custom policy to an entity mapping, the migration refuses to work for v2 -> v3.
我一直在尝试在我正在组合的应用程序中使用自定义迁移策略。到目前为止,使用映射模型时,迁移从v1 - > v2开始工作。但是,每当我向实体映射添加自定义策略时,迁移都拒绝为v2 - > v3工作。
Custom Migration Policy:
自定义迁移政策:
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
}
}
and when I run the app, the following error is returned:
当我运行该应用程序时,会返回以下错误:
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.])
with the important part being:
重要的是:
NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)
I've tried the few questions I have found on this problem, and none of them have provided any satisfactory solutions. I'd be very thankful if anybody could shed any light on the issue I'm facing!
我已经尝试了我在这个问题上找到的几个问题,但没有一个问题提供任何令人满意的解决方案。如果有人能对我所面临的问题有所了解,我将非常感激!
Thanks!
谢谢!
2 个解决方案
#1
27
I eventually solved this, after understanding an answer to a similar question. I had forgotten to add the project namespace, so rather than setting the custom policy name as ObjectCustomV2V3Migration
in the Mapping Model, I should have used ProjectModuleName.ObjectCustomV2V3Migration
.
在理解了类似问题的答案之后,我最终解决了这个问题。我忘了添加项目命名空间,因此我不应该在Mapping Model中将自定义策略名称设置为ObjectCustomV2V3Migration,而应该使用ProjectModuleName.ObjectCustomV2V3Migration。
#2
0
My issue was that my project name had spaces in it, so in the mapping model editor the custom policy should be specified using underscores instead of spaces when specifying the Swift module name.
我的问题是我的项目名称中有空格,所以在映射模型编辑器中,应该在指定Swift模块名称时使用下划线而不是空格来指定自定义策略。
For example, to specify the MyCustomMigrationPolicy
class in the project named My App Name, I used the following custom policy in the editor:
例如,要在名为My App Name的项目中指定MyCustomMigrationPolicy类,我在编辑器中使用了以下自定义策略:
My_App_Name.MyCustomMigrationPolicy
My_App_Name.MyCustomMigrationPolicy
#1
27
I eventually solved this, after understanding an answer to a similar question. I had forgotten to add the project namespace, so rather than setting the custom policy name as ObjectCustomV2V3Migration
in the Mapping Model, I should have used ProjectModuleName.ObjectCustomV2V3Migration
.
在理解了类似问题的答案之后,我最终解决了这个问题。我忘了添加项目命名空间,因此我不应该在Mapping Model中将自定义策略名称设置为ObjectCustomV2V3Migration,而应该使用ProjectModuleName.ObjectCustomV2V3Migration。
#2
0
My issue was that my project name had spaces in it, so in the mapping model editor the custom policy should be specified using underscores instead of spaces when specifying the Swift module name.
我的问题是我的项目名称中有空格,所以在映射模型编辑器中,应该在指定Swift模块名称时使用下划线而不是空格来指定自定义策略。
For example, to specify the MyCustomMigrationPolicy
class in the project named My App Name, I used the following custom policy in the editor:
例如,要在名为My App Name的项目中指定MyCustomMigrationPolicy类,我在编辑器中使用了以下自定义策略:
My_App_Name.MyCustomMigrationPolicy
My_App_Name.MyCustomMigrationPolicy