So I've made a function using FetchRequest to search, but im getting an error: "Accessing StateObject's object without being installed on a View. This will create a new instance each time." The function takes a string and searches the core data for objects containing that string. The string is obtained from a .searchable object and the function is ran as a ForEach parameter.
This is the function:
func searchResults(searchingFor: String) -> FetchedResults<Recipe> {
@FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)], predicate: NSPredicate(format: "title CONTAINS[c] %@",searchingFor)) var searchRecipes: FetchedResults<Recipe>
return searchRecipes
}
Is there a better way to do this or can I do it this way and tweak it a little bit? I really just need to have a search bar that returns a FetechedResults type so I can use it in a ForEach loop. Any help is greatly appreciated.