You can create Fetch Requests in the Xcode model editor, including a Predicate. These are somewhat easier to setup. To create one, select your .xcdatamodeld file, then head over to Editor – Add Fetch Request. Give it a catchy name and a filter property, and much of the above code becomes obsolete.
Here’s how you can retrieve one of those in code:
// create a fetch request from template NSFetchRequest *fetchRequest = [self.managedObjectModel fetchRequestTemplateForName:@"MyFetch"]; NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; for (Event *event in fetchedObjects) { NSLog(@"%@", [event.timeStamp description]); }
Note that to retrieve your Fetch Request Template you need a reference to your Managed Object Model as well as your context. The easiest way to grab hold of it in an iOS app is to pass it in on a property, just like we did with self.managedoObjectContext.