0

I am trying to save the data to CoreData using the saveSelectedQuestion function inside the CoreDataManager class, but I am getting the error found in the console output. I have given a custom model to the questions property as seen in the CoreData Entity. How can I save data to CoreData using a custom model. Can I use Dictionary while saving data in CoreData? [QuestionSections.RawValue : String]

You can review the model I added to the CoreData Entity below.

I can't save data to CoreData. Why ?

You can check this project on github link. It's a pretty simple one-page project. I just can't save data.

GitLab Project : Core Data Test Project

Console Output:

2021-08-21 20:59:52.440497+0300 CoreDataTest[34727:2823184] [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
2021-08-21 20:59:52.440702+0300 CoreDataTest[34727:2823184] -[CoreDataTest.QuestionList encodeWithCoder:]: unrecognized selector sent to instance 0x281574900
2021-08-21 20:59:52.440945+0300 CoreDataTest[34727:2823184] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
2021-08-21 20:59:52.441055+0300 CoreDataTest[34727:2823184] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-08-21 20:59:52.441137+0300 CoreDataTest[34727:2823184] [error] error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-08-21 20:59:52.441352+0300 CoreDataTest[34727:2823184] [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
2021-08-21 20:59:52.441465+0300 CoreDataTest[34727:2823184] -[CoreDataTest.QuestionList encodeWithCoder:]: unrecognized selector sent to instance 0x281574900
2021-08-21 20:59:52.441648+0300 CoreDataTest[34727:2823184] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
2021-08-21 20:59:52.441731+0300 CoreDataTest[34727:2823184] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-08-21 20:59:52.441799+0300 CoreDataTest[34727:2823184] [error] error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-08-21 20:59:52.441982+0300 CoreDataTest[34727:2823184] [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
2021-08-21 20:59:52.442071+0300 CoreDataTest[34727:2823184] -[CoreDataTest.QuestionList encodeWithCoder:]: unrecognized selector sent to instance 0x281574900
2021-08-21 20:59:52.442269+0300 CoreDataTest[34727:2823184] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
2021-08-21 20:59:52.442373+0300 CoreDataTest[34727:2823184] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e5c0c0> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-08-21 20:59:52.442451+0300 CoreDataTest[34727:2823184] [error] error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-08-21 20:59:52.453864+0300 CoreDataTest[34727:2823184] [general] 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
2021-08-21 20:59:52.454083+0300 CoreDataTest[34727:2823184] -[CoreDataTest.QuestionList encodeWithCoder:]: unrecognized selector sent to instance 0x281574900
2021-08-21 20:59:52.454309+0300 CoreDataTest[34727:2823184] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
2021-08-21 20:59:52.454399+0300 CoreDataTest[34727:2823184] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e58000> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x280e58000> , <shared NSKeyedUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
Failed to save selected category: A Core Data error occurred.

Core Data Entity:

enter image description here

Core Data Manager:

class CoreDataManager {
    static var shared = CoreDataManager()
    let persistentContainer: NSPersistentContainer
    
    init() {
        persistentContainer = NSPersistentContainer(name: "QuestionsCoreData")
        persistentContainer.loadPersistentStores { description, error in
            if let error = error {
                fatalError("Core Data Stre failed: \(error.localizedDescription)")
            }
        }
    }
    
    func searchInCategory(id: String) -> [QuestionCD] {
        let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
        let search = NSPredicate(format: "ANY id == %@", id)
        print("search: \(search)")
        fetchRequest.predicate = search
        
        do {
            return try persistentContainer.viewContext.fetch(fetchRequest)
        } catch {
            print("no Data \n")
            return []
        }
    }
    
    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)")
        }
    }
    
    func getSelectedQuestion(questionID: String) -> QuestionCD {
        let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
        let search = NSPredicate(format: "id == %@", questionID)
        print("search: \(search)")
        fetchRequest.predicate = search
        print("request predicate: \(String(describing: fetchRequest.predicate))")
        do {
            return try persistentContainer.viewContext.fetch(fetchRequest).first ?? QuestionCD()
        } catch {
            return QuestionCD()
        }
    }
}

My Model:

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, Hashable {
    case A = "A"
    case B = "B"
    case C = "C"
    case D = "D"
}
Ufuk Köşker
  • 1,288
  • 8
  • 29
  • Does this answer your question? ['NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release](https://stackoverflow.com/questions/62589985/nskeyedunarchivefromdata-should-not-be-used-to-for-un-archiving-and-will-be-re) – lorem ipsum Aug 21 '21 at 18:58
  • I tried this answer sir. :( this solution didn't run my code but it prints different error in console. – Ufuk Köşker Aug 21 '21 at 19:04
  • 1
    That is the answer you have to change to Secure coding. What you are using is going away and the items that the user has stored will be unreadable. You can't copy their code you have to implement similar code to fix yours. Add `NSSecureCoding` and create an `NSSecureUnarchiveFromDataTransformer` you will have a lot of adjusting to make it work – lorem ipsum Aug 21 '21 at 19:08
  • okey I will try sir. – Ufuk Köşker Aug 21 '21 at 19:10
  • @loremipsum tank you sir I solved this problem but I have encode problem. I asked here. Can you check ? https://stackoverflow.com/questions/68876969/core-data-threw-while-encoding-a-value-with-userinfo-of-null – Ufuk Köşker Aug 21 '21 at 21:46

0 Answers0