Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Fetch request from xcdatamodeld?

I'm noob with Core Data and I can't find the help I need on the Apple developer website.

I defined a fetch request in my .xcdatamodeld file but now I can't find how to use it? This is the definition of the fetch request : enter image description here

I supose that the fetch request starts with :

let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Category")

But I don't know how to use a fetch request by name. (The project is written in Swift 3)

like image 661
Maxime Avatar asked Nov 12 '16 09:11

Maxime


1 Answers

Bharath's answer is for how to create and execute a fetch request in code. This is commonly how you'll do it. However, it's not actually the answer to your question. If you actually create a fetch request named "allCategories" in the core data model like that, this is how you use it:

let fetchRequest = self.managedObjectModel.fetchRequestTemplate(forName: "allCategories")

let fetchedObjects = self.managedObjectContext.execute(fetchRequest!)
like image 189
The Cappy Avatar answered Nov 19 '22 01:11

The Cappy