CoreData所需的类型= NSManagedObject;给定类型= __NSSingleObjectSetI

时间:2019-01-25 14:45:13

标签: ios swift core-data database-migration core-data-migration

我正在使用NSEntityMigrationPolicy进行CoreData迁移,我需要在当前模型和迁移过程中创建的模型之间创建一种关系。我根据当前模型提供的数据在createDestinationInstances中创建了新模型,但是当我尝试建立关系时,却出现此错误desired type = NSManagedObject; given type = __NSSingleObjectSetI

关系是一对多的

override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
        let sourceAttributeKeys = Array(sInstance.entity.attributesByName.keys)
        let sourceAttributeValues = sInstance.dictionaryWithValues(forKeys: sourceAttributeKeys)

        guard let baseRelativeUrlString = sourceAttributeValues["baseRelativeUrlString"] as? String, let fontFileNames = sourceAttributeValues["fontFileNames"] as? [String] else {
            try super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager)
            return
        }

        // Create the destination Note instance
        let destinationInstance = NSEntityDescription.insertNewObject(forEntityName: mapping.destinationEntityName!, into: manager.destinationContext)

        // Get the destination attribute keys
        let destinationAttributeKeys = Array(destinationInstance.entity.attributesByName.keys)

        for key in destinationAttributeKeys {
            if let value = sourceAttributeValues[key] {
                destinationInstance.setValue(value, forKey: key)
            }
        }

        let appSupportUrl = FileSystemManager.appSupportURL
        var array = [CustomFontData]()
        for fontFileName in fontFileNames {
            let url = appSupportUrl.appendingPathComponent(baseRelativeUrlString).appendingPathComponent(fontFileName)
            do {
                let fontData = try Data(contentsOf: url)
                let fontDataName = fontFileName.components(separatedBy: ".")[0]
                let customFontData = CustomFontData(context: manager.destinationContext)
                customFontData.name = fontDataName
                customFontData.data = fontData
                array.append(customFontData)
            } catch {
                debugPrint(#function, error.localizedDescription)
                BugHunter.shared.catchError(error, devInfo: [.functionName : #function])
            }
        }
        destinationInstance.setValue(NSSet(array: array), forKey: "fontsData")

        manager.associate(sourceInstance: sInstance, withDestinationInstance: destinationInstance, for: mapping)
    }

1 个答案:

答案 0 :(得分:0)

我用NSEntityDescription替换了自定义类,并使用了KVO。

override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
        let sourceAttributeKeys = Array(sInstance.entity.attributesByName.keys)
        let sourceAttributeValues = sInstance.dictionaryWithValues(forKeys: sourceAttributeKeys)

        guard let baseRelativeUrlString = sourceAttributeValues["baseRelativeUrlString"] as? String, let fontFileNames = sourceAttributeValues["fontFileNames"] as? [String] else {
            try super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager)
            return
        }

        // Create the destination Note instance
        let destinationInstance = NSEntityDescription.insertNewObject(forEntityName: mapping.destinationEntityName!, into: manager.destinationContext)

        // Get the destination attribute keys
        let destinationAttributeKeys = Array(destinationInstance.entity.attributesByName.keys)

        for key in destinationAttributeKeys {
            if let value = sourceAttributeValues[key] {
                destinationInstance.setValue(value, forKey: key)
            }
        }

        let appSupportUrl = FileSystemManager.appSupportURL
        var array = [NSManagedObject]()
        for fontFileName in fontFileNames {
            let url = appSupportUrl.appendingPathComponent(baseRelativeUrlString).appendingPathComponent(fontFileName)
            do {
                let fontData = try Data(contentsOf: url)
                let fontDataName = fontFileName.components(separatedBy: ".")[0]
                let customFontData = NSEntityDescription.insertNewObject(forEntityName: CustomFontData.entityName, into: manager.destinationContext)
                customFontData.setValue(fontDataName, forKey: "name")
                customFontData.setValue(fontData, forKey: "data")
                array.append(customFontData)
            } catch {
                debugPrint(#function, error.localizedDescription)
                BugHunter.shared.catchError(error, devInfo: [.functionName : #function])
            }
        }
        destinationInstance.setValue(NSSet(array: array), forKey: "fontsData")

        manager.associate(sourceInstance: sInstance, withDestinationInstance: destinationInstance, for: mapping)
    }