[Core Data]: threw while encoding a value. with userInfo of (null)

I am getting the following error while saving data to CoreData. my model has the dictionary property. I created NSSecureCoding and NSSecureUnarchiveFromDataTransformer classes. please check if i can encode my model correctly. I couldn't understand what I am doing wrong ? The problem is not with Dictionary or Data properties. Why can't I save data to CoreData? I know little English. Thank you for your understanding.

GitLab Project Link:

It is a very simple one page project. I will be glad if you check it out.

Core Data Test Project

Console Output:

2021-08-21 23:58:18.490834+0300 CoreDataTest[35689:2901360] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2827f0240> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2827f0240> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
Failed to save selected category: A Core Data error occurred.

Core Data Entity:

My Custom Model:

Notice my model has a Dictionary.

var sections: [QuestionSections.RawValue : String]

I am trying to save dictionary in CoreData. I thought the problem was in the dictionary but the problem is not in the dictionary.

class QuestionContainer: NSObject, Codable {
    
    var questionCategories: [Question]
    
    init(questionCategories: [Question]) {
        self.questionCategories = questionCategories
    }
}

public class Question: NSObject, Codable {
    
    var title: String
    var id: String
    var questions: [QuestionList]
    
    init(title: String, id: String, questions: [QuestionList]) {
        self.title = title
        self.id = id
        self.questions = questions
    }
}

public class QuestionList: NSObject, Codable {
    
    init(id: String, question: String, isQuestionImage: Bool, isSectionImage: Bool, imageURL: String, imageData: Data? = nil, sections: [QuestionSections.RawValue : String], selected: String, correct: String) {
        self.id = id
        self.question = question
        self.isQuestionImage = isQuestionImage
        self.isSectionImage = isSectionImage
        self.imageURL = imageURL
        self.imageData = imageData
        self.sections = sections
        self.selected = selected
        self.correct = correct
    }
    
    var id: String
    var question: String
    var isQuestionImage, isSectionImage: Bool
    var imageURL: String
    var imageData: Data?
    var sections: [QuestionSections.RawValue : String]
    var selected: String
    var correct: String
}

enum QuestionSections: String, Codable {
    case A = "A"
    case B = "B"
    case C = "C"
    case D = "D"
}

NSSecure Unarchive From Data Transformer:

@objc(QuestionsValueTransformer)
class QuestionsValueTransformer: NSSecureUnarchiveFromDataTransformer {
    static let name = NSValueTransformerName(rawValue: String(describing: QuestionsValueTransformer.self))
 
    override static var allowedTopLevelClasses: [AnyClass] {
        return [QuestionsNSSecureCoding.self]
    }
    
    public static func register() {
            let transformer = QuestionsValueTransformer()
            ValueTransformer.setValueTransformer(transformer, forName: name)
        }
}

CoreData Save Function:

func saveSelectedQuestion(questionTitle: String, id: String, questions: [QuestionList]) {
    
    let questionsCD = QuestionCD(context: persistentContainer.viewContext)
    questionsCD.title = questionTitle
    questionsCD.id = id
    questionsCD.questions = questions
    
    do {
        try persistentContainer.viewContext.save()
    } catch {
        print("Failed to save selected category: \(error.localizedDescription)")
    }
}

Replies

Did you ever get the bottom of this? I'm having the same issue.