FetchedResults to Array (Error)

So I've found a way to convert fetched results to an array of the same data type, and not only that but filter them with the fetch request given a string:

func searchResults(searchingFor: String)->[Recipe]{
    var filteredRecipeList=[Recipe]()
    @FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)], predicate: NSPredicate(format: "title CONTAINS[c] %@",searchingFor)) var filteredResults: FetchedResults<Recipe>

    for recipe in filteredResults {
        filteredRecipeList.append(recipe)
    }
    return filteredRecipeList
}

To clarify, this would ideally return an array with a list of Recipes that contain the given string in the title. In theory this should work just fine, but I'm getting a weird error. I've never seen an error like this and I'm not sure how to understand it. It is purple with a yellow warning. The error says "Accessing StateObject's object without being installed on a View. This will create a new instance each time." How do I get around this issue to accomplish what I'm trying to accomplish.

Thanks in advance for any help whatsoever. I'll upvote anyone with any bit of helpful information. Have a good one!

Answered by Zimmie in 717724022

You can't use @FetchRequest in a function, but you absolutely can use a fetch request in a function. You just have to build an NSFetchRequest object, set its predicate and sort descriptors, then fetch it from a managed object context. Depending on exactly where that function is in your code, you may be able to access the managed object context without passing it explicitly into the function. It would go a bit like this:

func searchResults(searchingFor: String)
-> [Recipe] {
	let recipeFetch = Recipe.fetchRequest()
	recipeFetch.predicate = NSPredicate(format: "title CONTAINS[c] %@",searchingFor)
	recipeFetch.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
	let results = (try? self.managedObjectContext.fetch(recipeFetch) as [Recipe]) ?? []
	return results
}

I haven't run this exact code, but it's very similar to code I'm using in several of my applications.

Okay so I’ve finally found somewhere else that said you can’t use a fetch request outside of a view. But if I pass in the fetch request made in the view as a parameter I wouldn’t be able to use the dynamic string searchingFor in the declaration of my FetchRequest. Any ideas?

Accepted Answer

You can't use @FetchRequest in a function, but you absolutely can use a fetch request in a function. You just have to build an NSFetchRequest object, set its predicate and sort descriptors, then fetch it from a managed object context. Depending on exactly where that function is in your code, you may be able to access the managed object context without passing it explicitly into the function. It would go a bit like this:

func searchResults(searchingFor: String)
-> [Recipe] {
	let recipeFetch = Recipe.fetchRequest()
	recipeFetch.predicate = NSPredicate(format: "title CONTAINS[c] %@",searchingFor)
	recipeFetch.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
	let results = (try? self.managedObjectContext.fetch(recipeFetch) as [Recipe]) ?? []
	return results
}

I haven't run this exact code, but it's very similar to code I'm using in several of my applications.

FetchedResults to Array (Error)
 
 
Q