79093436

Date: 2024-10-16 09:47:02
Score: 1
Natty:
Report link

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!

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @JoakimDanielson
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Ulrich