Get Updates By E-mail 

Article Written By:
Johann Dowa

iOS Library For Simplifying Complex Core Data Queries Inspired By Microsoft .NET’s LINQ

  •  
  •  
  • 1
  •  
  •  

I’ve mentioned a number of excellent tools and libraries for working with Core Data, most recently the excellent PonyDebugger editor.

Here’s a new library from Marty Dill called iOS-Queryable that aims to simplify the creation of complex Core Data queries.

The library allows you to eliminate most of the boiler plate code when created complex core data queries by bringing a subset of methods from the IQueryable and IEnumerable libraries from Microsoft.NET LINQ framework.

This is best shown with an example and the example given on the readme is that with this library you can write code like this:

NSArray* widgets = [[[[[self.managedObjectContext ofType:@"Widget"]
            where:@"Type == 'abc'"]
            orderBy:@"createddate"]
            take:5]
            toArray];

Instead of:

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];

You can find iOS-Queryable on Github here.

There’s much more to the library than what’s been shown here, and you’ll definitely want to check it out if you find yourself writing some complex core data queries.


  •  
  •  
  • 1
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments