Core Data query helper

THIS ARTICLE HAS BEEN REVISED. YOU CAN FIND THE NEWER VERSION HERE!

I’ve recently started using Core Data quite a bit for a few different projects and quickly found this bit of code and modified it slightly for my own use. It was originally posted by Björn Sållarp through his blog, on a helpful post about how to use Core Data and I’ve continued to use it since in different formats.

The code basically returns a data set from Core Data (with or without a predicate) without having all the messy code for each query. Here’s the code…

CoreDataHelper.h

//  CoreDataHelper.h
//
//  Modified by Simon on 09/10/2010.
//
//  ----------------------------
//
//  MODIFIED version of this...
//
//  Created by Björn Sållarp on 2009-06-14.
//  NO Copyright 2009 MightyLittle Industries. NO rights reserved.
//
//  Use this code any way you like. If you do like it, please
//  link to my blog and/or write a friendly comment. Thank you!
//
//  Read my blog @ http://blog.sallarp.com

#import
#import 

@interface CoreDataHelper : NSObject {
}

+(NSMutableArray *)getObjectsForEntity:(NSString*)entityName withSortKey:(NSString*)sortKey andSortAscending:(BOOL)sortAscending andContext:(NSManagedObjectContext *)managedObjectContext;
+(NSMutableArray *)searchObjectsForEntity:(NSString*)entityName withPredicate:(NSPredicate *)predicate andSortKey:(NSString*)sortKey andSortAscending:(BOOL)sortAscending andContext:(NSManagedObjectContext *)managedObjectContext;

@end

CoreDataHelper.m

//  CoreDataHelper.m
//
//  Modified by Simon on 09/10/2010.
//
//  ----------------------------
//
//  MODIFIED version of this...
//
//  Created by Björn Sållarp on 2009-06-14.
//  NO Copyright 2009 MightyLittle Industries. NO rights reserved.
//
//  Use this code any way you like. If you do like it, please
//  link to my blog and/or write a friendly comment. Thank you!
//
//  Read my blog @ http://blog.sallarp.com

#import "CoreDataHelper.h"

@implementation CoreDataHelper

+(NSMutableArray *)searchObjectsForEntity:(NSString*)entityName withPredicate:(NSPredicate *)predicate andSortKey:(NSString*)sortKey andSortAscending:(BOOL)sortAscending andContext:(NSManagedObjectContext *)managedObjectContext
{

	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
	[request setEntity:entity];	

	// If a predicate was passed, pass it to the query
	if(predicate != nil)
	{
		[request setPredicate:predicate];
	}

	// If a sort key was passed, use it for sorting.
	if(sortKey != nil)
	{
		NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
		NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
		[request setSortDescriptors:sortDescriptors];
		[sortDescriptors release];
		[sortDescriptor release];
	}

	NSError *error;

	NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];

	[request release];

	return mutableFetchResults;
}

+(NSMutableArray *)getObjectsForEntity:(NSString*)entityName withSortKey:(NSString*)sortKey andSortAscending:(BOOL)sortAscending andContext:(NSManagedObjectContext *)managedObjectContext
{
	return [self searchObjectsForEntity:entityName withPredicate:nil andSortKey:sortKey andSortAscending:sortAscending andContext:managedObjectContext];
}

@end

And to use the helper, simply #import “CoreDataHelper.h” at the top of your file, then use something like this to get data…

// Get some data using the helper
NSMutableArray *mutableFetchResults = [CoreDataHelper getObjectsForEntity:@"MyEntityName" withSortKey:@"sortField" andSortAscending:NO andContext:managedObjectContext];

I posted this up because it’s incredibly useful and makes sense to have it somewhere else on the internet. If you’d like to read more stuff by Björn Sållarp, you can read his blog here.

Leave a Reply