My current project relies heavily on Core Data. Since writing queries with Core Data really sucks, I have thrown together a little helper project called ios-queryable to ease the pain.
From the readme:
ios-queryable is an Objective-C category that provides IQueryable and IEnumerable-like functionality to Core Data.
It supports query composition and deferred execution, and implements a subset of IEnumerable’s methods, such as where, take, skip, and orderBy.
It lets you write code like this:
NSArray* widgets = [[[[[self.managedObjectContext ofType:@"Widget"]
where:@"Type == 'abc'"]
orderBy:@"createddate"]
take:5]
toArray];
instead of like this:
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription
entityForName:@"Widget" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate* predicate = [NSPredicate predicateWithFormat: @"type == 'abc'"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"createddate" ascending:YES];
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchLimit:5];
NSError* error;
NSArray* widgets = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
Please send me your feedback, and keep in mind that ios-queryable is still a work in progress!
Great article! I will be using this in an application very soon.
Awesome, when you are done, I’d love to hear how you made out!