Solved! I fetched the wrong data type. The correct implementation of the getFoodItemByID function must be:
static func getFoodItemByID(id: UUID) -> FoodItem? {
let predicate = NSPredicate(format: "id == %@", id as CVarArg)
let request: NSFetchRequest<FoodItem> = FoodItem.fetchRequest()
request.predicate = predicate
do {
let result = try CoreDataStack.viewContext.fetch(request)
if !result.isEmpty {
return result[0]
}
} catch {
debugPrint("Error fetching food item: \(error)")
}
return nil
}
Kudos to @JoakimDanielson for the right hint!