I'm working on core data with extensions, and having errors below.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: is not a valid NSFetchRequest.'
It's working well on host app still, however crashing on extensions with the same codes.
Debugger stopped when executed managedObjectContext.fetch(request)
func getDataString() -> String {
var result = ""
let entityDescription = NSEntityDescription.entity(forEntityName: "DataString", in: managedObjectContext)!
let request : NSFetchRequest<NSFetchRequestResult> = DataString.fetchRequest()
request.entity = entityDescription
do {
let results = try managedObjectContext.fetch(request)
printDebug(message: "\tDataString Count : \(results.count)")
if results.count > 0 {
let row = results[0] as! NSManagedObject
result = row.value(forKey: "data") as! String
}
} catch let error {
printDebug(message: "ERROR : getDataString fetch")
setErrorLog(errorText: "\(error)")
}
return result
}
This crashes even though written with try-catch. Please help.
Answer myself.
func getDataString() -> String {
var result = ""
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "DataString")
do {
let results = try managedObjectContext.fetch(request)
printDebug(message: "\tDataString Count : \(results.count)")
if results.count > 0 {
let row = results[0] as! NSManagedObject
result = row.value(forKey: "data") as! String
}
} catch let error {
printDebug(message: "ERROR : getDataString fetch")
setErrorLog(errorText: "\(error)")
}
return result }
This worked.