Shourob Datta
Shourob Datta

Reputation: 2072

Transformable attribute in Core Data not export/sync with iCloud

I am using NSPersistentCloudKitContainer. All data sync in iCloud. And when uninstalled the app, its sync correctly.

However, it's required to encrypt sensitive data. I made my password attribute Transformable. Thus work fine locally. However when I delete the app. Install again. All other attributes(String, Int) sync correctly but transformable attribute does not export/synced.

Here is my Model enter image description here

public class EmployeeEntity: NSManagedObject {

    @NSManaged public var password: NSObject?

    @NSManaged public var firstName: String?
    @NSManaged public var lastName: String?
    @NSManaged public var idNumber: String?
    @NSManaged public var modifiedDate: Date?
    @NSManaged public var password: String?
    @NSManaged public var title: String?

    static var entityName: String { return "EmployeeEntity" }
}


extension EmployeeEntity {

    static func getListItemFetchRequest() -> NSFetchRequest<EmployeeEntity>{
        let request = EmployeeEntity.fetchRequest() as! NSFetchRequest<EmployeeEntity>
        request.sortDescriptors = [NSSortDescriptor(key: "createdDate", ascending: false)]
        return request
    }
}

import UIKit

class EncryptionTransformer: ValueTransformer {

    override class func allowsReverseTransformation() -> Bool{
        return true
    }
    override class func transformedValueClass() -> AnyClass {
        return NSData.self
    }
    override func transformedValue(_ value: Any?) -> Any?{
        guard let date = value as? String else {return nil}

        return date.data(using: .utf8)?.base64EncodedData()
    }

    override func reverseTransformedValue(_ value: Any?) -> Any?{
        guard let data = value as? String, let decoded = Data(base64Encoded: data) else {return nil}

        return String(data: decoded, encoding: .utf8)
    }
}

Do you have any idea why Transformable value not saved/sync from iCloud?

Upvotes: 1

Views: 333

Answers (0)

Related Questions