Created
July 4, 2013 06:36
-
-
Save Marlunes/5925369 to your computer and use it in GitHub Desktop.
FETCH REQUEST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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