Home > Mobile >  CoreData with transformable attribute fails on save
CoreData with transformable attribute fails on save

Time:10-01

I want to use CoreData and save a custom class attribute in one of the entities. The way to do that seems to be to use Transformable:

enter image description here

And then I try to add a single element and save it as:

import UIKit;
import CoreData;

public class EntityData: NSObject, NSSecureCoding
{
    public static var supportsSecureCoding: Bool { true }

    public func encode(with coder: NSCoder) {
        coder.encode(self.name, forKey: "name");
    }

    public required init?(coder: NSCoder) {
        self.name = coder.decodeObject(forKey: "name") as? String;
    }

    var name: String?;

    init(name: String?) {
        self.name = name;
    }
}

class ViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(_: animated);

        let context  = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext;
        
        let entity = Entity(context: context);
        
        entity.id = URL(string: "google.com");
        entity.data = EntityData(name: "Foo");
        
        do {
            try context.save();
        } catch {
            print("While saveing: \(error)");
        }
    }
}

This fails with:

2021-09-30 13:12:58.865312 0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.865530 0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.865983 0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.866158 0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.866558 0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.900366 0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.912131 0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
While saveing: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred."

I am not sure what I am doing wrong. This is all the code I have, there is nothing else going on that I've added.

CodePudding user response:

A transformable attribute requires a class conforming to NS(Secure)Coding (not Codable!) and the implementation of archiving the object(s). Note the NSSecureUnarchiveFromData errors.

In the Swift world consider to declare the attribute as String, encode the object to JSON with Codable and add a computed property for the seamless conversion.

  • Related