Tuesday May 10, 2011

One Line Core Data Fetch

This is an oldie but a goodie. Matt Gallagher has a great post on reducing common core data fetches to one line. How’s this for sexy?

[managedObjectContext fetchObjectsForEntityName:@"Employee"
    withPredicate: @"(lastName LIKE[c] 'Worsley') AND (salary > %@)",

Well, sexy as far as Objective-C goes. ;)

The best part: he shows you how you can have the method respond differently depending on the class type of a parameter. You can hand in an NSString predicate with interpolated values as shown above, or you can hand in a fully instantiated NSPredicate object.