How to resolve a SwiftData PredicateExpressions error
10:41 17 Feb 2024

I'm trying to save Category objects in SwiftData:

@Model
class Category: Decodable, Identifiable {
    var id: Int
    var title: String
}

In a viewModel I'm doing this:

init(modelContext: ModelContext) {
    self.modelContext = modelContext
    Task {
        categories = await SomeManager.shared.parseCategories() //from json
        for category in categories {
            categoryFetchDescriptor.predicate = #Predicate { item in
                return item.id == category.id
            }
            
            do {
                let existingCategory = try modelContext.fetch(categoryFetchDescriptor)
                
                if existingCategory.first == nil {
                    modelContext.insert(category)
                }
            } catch {
                print("SwiftData Error: \(error)")
            }
        }
        setup()
    }
}

When I try to run the app, I get this Xcode error:

Cannot convert value of type 'PredicateExpressions.Equal, PredicateExpressions.KeyPath>' to closure result type 'any StandardPredicateExpression'

Why is it having trouble comparing Ints?

swift swiftui swiftdata