Fetching Managed Objects

To work with existing data from a managed object context, you’ll first need to fetch it. If the data isn’t already in a context when fetched, it will be retrieved from the underlying persistent store transparently. To fetch, you’ll need an instance of NSFetchRequest, which will return an NSArray of managed objects. When the fetch is executed, every managed object for the specified entity will be returned in the resulting array. In SQL database terms, a fetch is similar to a SELECT statement. The code involved is shown in Listing 2.4.

Listing 2.4 AppDelegate.m: demo (Fetch Request)

NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"Item"];[_coreDataHelper.context executeFetchRequest:request ...

Get Learning Core Data for iOS: A Hands-On Guide to Building Core Data Applications 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.