Tuesday, August 30, 2011

Core Data Tutorial: Filtering

-(void)searchPerson:(Person*)person inContext:(NSManagedObjectContext *)context{

//Entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext: context];

//Request
NSFetchRequest *fetchRequest = [NSFetchRequest alloc] init];
[fetchRequest setEntity: entity];

//predicate
NSPredictate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name='%@'",nameSearch]];
[fetchRequest setPredicate: predicate];

//results
NSArray *person = [context excuteFetchRequest: fetchRequest error:nil];

}

Core Data Tutorial: Create Data

-(void)createPerson:(Person *)person inContext:(NSManagedContext*)context{

//Create new Object

NSManagedObject * newPerson = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext: context];

//String Value
[newPerson setValue:[person name] forKey:@"name"];

//Id Value (index)
int personID = [newPerson hash];
[newPerson setValue:[NSNumber numberWithInt: personID] forKey:@"id"];
}