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:
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"
}