I need to be able to search my list of recipes given a string. I’m unsure how to filter a data model in core data though. I tried a ForEach(container){ recipe in, but it said it gave me an error. Anyone know the correct way in the scrappy way doesn’t work? Thanks
data controller:
import Foundation
import CoreData
class DataController: ObservableObject {
let container = NSPersistentContainer(name: "RecipeModel")
init() {
container.loadPersistentStores { desc, error in
if let error = error {
print("Failed to load the data \(error.localizedDescription)")
}
}
}
func save(context: NSManagedObjectContext) {
do {
try context.save()
print("Data saved")
} catch {
print("The data could not be saved")
}
}
func addRecipe(title: String, ingredients: [String], instructions: [String], notes: String, context: NSManagedObjectContext){
let recipe = Recipe(context: context)
recipe.id = UUID()
recipe.date = Date()
recipe.title = title
recipe.ingredients = ingredients
recipe.instructions = instructions
recipe.notes = notes
save(context: context)
}
func editRecipe(recipe: Recipe, title: String, ingredients: [String], instructions: [String], notes: String, context: NSManagedObjectContext){
recipe.title = title
recipe.ingredients = ingredients
recipe.instructions = instructions
recipe.notes = notes
save(context: context)import Foundation
}
func updateDate(recipe: Recipe, context: NSManagedObjectContext){
recipe.date=Date()
save(context: context)
}
}
research @FetchRequest