我正在尝试在应用程序最初启动时从Web服务中保存一堆“缺勤”(登录后无法直接在AppDelegate中执行此操作)。
我有以下代码可以找到任何现有记录并在获取新集之前删除它们。
fetch工作正常(虽然没有任何东西可以提取),因为我看到打印的行'Nothing found'。但是当我从Web服务循环结果时,我从行'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'AbsenceTypes' must have a valid NSEntityDescription.'
let newAbsence = AbsenceTypes(context: context)
func getAndSaveAppointmentTypes(){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
absences = try context.fetch(AbsenceTypes.fetchRequest())
} catch {
print("Nothing found")
}
if !absences.isEmpty {
for absence in absences {
context.delete(absence)
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
let paramString : String = "My Param String"
DataFunctions().getRequest(methodName: "MyMethod", parameters: paramString) { (result) -> () in
for item in result {
if item["Error"] != nil {
handleError()
return
} else {
let absenceID : String = item["id"] as! String
let absenceDesc : String = item["Description"] as! String
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newAbsence = AbsenceTypes(context: context)
newAbsence.absenceDesc = absenceDesc
newAbsence.absenceId = absenceID
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
print("Absences saved")
}
}
}
到目前为止,我有: