Skip to content

Instantly share code, notes, and snippets.

@Marlunes
Created July 4, 2013 06:36
Show Gist options
  • Save Marlunes/5925369 to your computer and use it in GitHub Desktop.
Save Marlunes/5925369 to your computer and use it in GitHub Desktop.
FETCH REQUEST
/*
To call:
myArray = [self fetchDataWithEntityName:@"Employees" withSortDescriptorKey:@"name" withPredicateFormat:[NSPredicate predicateWithFormat:@"employee == %@", myEmployeeName];
*/
- (NSArray *)fetchDataWithEntityName:(NSString *)entityName withSortDescriptorKey:(NSString *) sortDescriptorKey withPredicateFormat:(NSPredicate *)predicateFormat{
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:sortDescriptorKey
ascending:YES];
NSArray *descriptor = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:descriptor];
[request setEntity:entity];
[request setPredicate:predicateFormat];
NSError *error;
NSArray *resultArray = [context executeFetchRequest:request error:&error];
if(error){
return nil;
} else {
return resultArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment