Building an NSFetchRequest

NSFetchRequest is the part of Core Data that causes people to think it’s a database API instead of an object hierarchy. When we want to retrieve objects from Core Data, we normally use an NSFetchRequest to do the retrieval. It’s best to view an NSFetchRequest as a way to retrieve all instances of an entity from the object hierarchy, with the option to filter the results with an NSPredicate. There are two parts to the creation of an NSFetchRequest: setting the entity to be retrieved and optionally defining an NSPredicate to filter the objects that we want retrieved.

Setting the Entity

One thing that we must do as part of every NSFetchRequest is define the entity we want returned from the fetch. We do this by passing ...

Get Core Data in Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.