December 2012
Intermediate to advanced
834 pages
27h
English
You want to be able to read the contents of your entities (tables) using Core Data.
Use an instance of NSFetchRequest:
-(BOOL)createNewPersonWithFirstName:(NSString*)paramFirstNamelastName:(NSString*)paramLastNameage:(NSUInteger)paramAge{BOOLresult=NO;if([paramFirstNamelength]==0||[paramLastNamelength]==0){NSLog(@"First and Last names are mandatory.");returnNO;}Person*newPerson=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Person"inManagedObjectContext:self.managedObjectContext];if(newPerson==nil){NSLog(@"Failed to create the new person.");returnNO;}newPerson.firstName=paramFirstName;newPerson.lastName=paramLastName;newPerson.age=[NSNumbernumberWithUnsignedInteger:paramAge];NSError*savingError=nil;if([self.managedObjectContextsave:&savingError]){returnYES;}else{NSLog(@"Failed to save the new person. Error = %@",savingError);}returnresult;}-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{[selfcreateNewPersonWithFirstName:@"Anthony"lastName:@"Robbins"age:51];[selfcreateNewPersonWithFirstName:@"Richard"lastName:@"Branson"age:61];/* Create the fetch request first */NSFetchRequest*fetchRequest=[[NSFetchRequestalloc]init];/* Here is the entity whose contents we want to read */NSEntityDescription*entity=[NSEntityDescriptionentityForName:@"Person"inManagedObjectContext:self.managedObjectContext ...
Read now
Unlock full access