Search code examples
iosswiftcore-datadatabase-migrationcore-data-migration

CoreData desired type = NSManagedObject; given type = __NSSingleObjectSetI


I'm doing CoreData migration using NSEntityMigrationPolicy, I need to create a relationship between the current model and the ones created during the migration. I create new models in createDestinationInstances from the data given by the current model, but when I try to establish relationships, I get this error desired type = NSManagedObject; given type = __NSSingleObjectSetI

Relationship is one to many

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)
    }

Solution

  • I replaced the custom class with NSEntityDescription and used 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)
        }