核心数据迁移:无法将“NSManagedObject_MyType”类型的值转换为“MyModule.MyType”

hor*_*oe7 2 migration core-data ios swift

我正在进行“中等重量”核心数据迁移。我使用映射模型从一个旧存储/数据模型迁移到不同的存储和不同的模型(即完全不同的.xcdatamodeld)文件,并NSEntityMigrationPolicy在适用的情况下使用自定义对象。

以前我在对象图上有各种不相关的对象,现在我想要一个主对象,Library它使我能够轻松擦除所有关联的数据(使用级联删除规则)。

由于子类中的自定义方法,我在迁移过程中遇到了问题NSEntityMigrationPolicy

class LegacyToModernPolicy: NSEntityMigrationPolicy {

func libraryForManager(_ manager: NSMigrationManager) -> Library {

    let fetchRequest: NSFetchRequest<Library> = NSFetchRequest(entityName: Library.entity().name!)

    fetchRequest.predicate = nil
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "filename", ascending: true)]
    fetchRequest.fetchLimit = 1

    do {
        // will fail here if NSFetchRequest<Library>
        let results = try manager.destinationContext.fetch(fetchRequest)  
        log.info("results: \(results)")

        if results.count == 1 {
            // can fail here if NSFetchRequest<NSManagedObject>
            return results.first! as! Library  
        } else {
            let newLib = Library(context: manager.destinationContext)
            return newLib
        }

    } catch {
        log.error("Error fetching: \(error.localizedDescription)")
    }

    let newLib = Library(context: manager.destinationContext)
    return newLib
  }
}
Run Code Online (Sandbox Code Playgroud)

会抛出异常,错误信息为:

Could not cast value of type 'NSManagedObject_Library_' (0x6100000504d0) to 'SongbookSimple.Library' (0x101679180).

问题是,为什么会发生这种情况,这重要吗?因为正在发生迁移,也许返回NSManagedObject具有正确实体描述的 就足够了?

hor*_*oe7 5

原因是在迁移期间,您不应该使用 NSManagedObject 子类的实例。您需要将所有这些都以 NSManagedObject 的形式表达。所以上面的代码必须变成:

class LegacyToModernPolicy: NSEntityMigrationPolicy {

static func find(entityName: String,
                 in context: NSManagedObjectContext,
                 sortDescriptors: [NSSortDescriptor],
                 with predicate: NSPredicate? = nil,
                 limit: Int? = nil) throws -> [NSManagedObject] {

    let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: entityName)
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = sortDescriptors
    if let limit = limit {
        fetchRequest.fetchLimit = limit
    }

    do {
        let results = try context.fetch(fetchRequest)
        return results
    } catch {
        log.error("Error fetching: \(error.localizedDescription)")
        throw error
    }
}

func libraryForManager(_ manager: NSMigrationManager) -> NSManagedObject {

    do {
        var library: NSManagedObject? = try LegacyToModernPolicy.find(entityName: Library.entity().name!,
                                                in: manager.destinationContext,
                                                sortDescriptors: [NSSortDescriptor(key: "filename", ascending: true)],
                                                with: nil,
                                                limit: 1).first
        if library == nil {
            let dInstance = NSEntityDescription.insertNewObject(forEntityName: Library.entity().name!, into: manager.destinationContext)

            // awakeFromInsert is not called, so I have to do the things I did there, here:
            dInstance.setValue(Library.libraryFilename, forKey: #keyPath(Library.filename))
            dInstance.setValue(NSDate(timeIntervalSince1970: 0), forKey: #keyPath(Library.updatedAt))
            library = dInstance
        }

        return library!

    } catch {
        fatalError("Not sure why this is failing!")
    }
}}
Run Code Online (Sandbox Code Playgroud)

您可以在此处详细了解我在核心数据迁移方面的不那么有趣的经历。