Skip to content

Instantly share code, notes, and snippets.

@shannoga
Created May 30, 2011 07:29
Show Gist options
  • Save shannoga/998559 to your computer and use it in GitHub Desktop.
Save shannoga/998559 to your computer and use it in GitHub Desktop.
Core data fetch function
+(NSArray*)fetchForCardsEntity:(NSString*)entityName withPredicate:(NSString*)predicateFormat withSortDiscriptor:(NSString*)sortdDscriptorName{
NSManagedObjectContext *moc=[[self appDelegate] managedObjectContext];
NSEntityDescription *entityDescription;
NSPredicate *predicate;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
[request setEntity:entityDescription];
predicate = [NSPredicate predicateWithFormat: predicateFormat];
[request setPredicate:predicate];
if (sortdDscriptorName) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:sortdDscriptorName ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
}
NSError *error = nil;
NSArray * requestArray =[moc executeFetchRequest:request error:&error];
if (requestArray == nil)
{
// Deal with error...
}
[request release];
return requestArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment