【问题标题】:Couldn't create mapping policy - iOS Core Data无法创建映射策略 - iOS Core Data
【发布时间】:2015-07-10 18:20:48
【问题描述】:

我一直在尝试在我正在整合的应用程序中使用自定义迁移策略。到目前为止,在使用映射模型时,迁移已经从 v1 -> v2 开始。但是,每当我向实体映射添加自定义策略时,迁移都拒绝适用于 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)

我已经尝试了我在这个问题上找到的几个问题,但没有一个提供任何令人满意的解决方案。如果有人能阐明我所面临的问题,我将不胜感激!

谢谢!

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

    在了解了类似问题的答案后,我最终解决了这个问题。我忘记添加项目命名空间,所以与其在映射模型中将自定义策略名称设置为ObjectCustomV2V3Migration,我应该使用ProjectModuleName.ObjectCustomV2V3Migration

    【讨论】:

    • 我也解决了,但是我改变了我的实现。在我的情况下,我可以使用 Filter predicate 选项而不是自定义策略。准备破译你得到的错误
    • 很高兴你把它整理出来 :) 我希望 XCode 有更多有用的错误消息,或者至少有更详细的解释!
    • 非常感谢 :) 挣扎了 4-5 个小时。
    • 值得链接到项目名称 -> Swift 模块名称映射developer.apple.com/library/content/documentation/Swift/…
    • 您将如何使用多个目标来扩展它?到目前为止,我尝试使用 $(PRODUCT_MODULE_NAME) 没有任何运气......
    【解决方案2】:

    我的问题是我的项目名称中有空格,因此在映射模型编辑器中,在指定 Swift 模块名称时,应使用下划线而不是空格来指定自定义策略。

    例如,要在名为 My App Name 的项目中指定 MyCustomMigrationPolicy 类,我在编辑器中使用了以下自定义策略:

    My_App_Name.MyCustomMigrationPolicy

    【讨论】:

    • 如果您的项目名称有“-”,那么它们也应该转换为“_”
    • 我的项目名称有“.”在里面,我还需要替换“。”用“_”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多