I try to get an Entity via an NSPredicate from CoreData model. Initially I insert an entity using its id
and now intend to get it. However my code crashes without any further description on specifically my NSPredicate
. I have no idea why? My id
attribute is Int32
:
var employTime:EmployeeTime?
let context = getContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EmployeeTime")
//code crashes on this bottom line
let predicate = NSPredicate(format: "id == %@", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest) as? [EmployeeTime]
if (employTimes?.count)! > 0 {
for et in employTimes! {
if et.id == id {
employTime = et
}
}
} else {
employTime = EmployeeTime(context: context)
}
return employTime!
Any help most greatly appreciated.