December 2012
Intermediate to advanced
834 pages
27h
English
You want to delete a managed object (a row in a table) from a managed object context (your database).
Use the deleteObject:
instance method of NSManagedObjectContext:
-(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];NSSortDescriptor*ageSort=[[NSSortDescriptoralloc]initWithKey:@"age"ascending:YES];/* Tell the request that we want to read thecontents of the Person entity */[fetchRequestsetEntity:entity];NSError*requestError=nil;/* And execute the fetch request on the context */NSArray*persons=[self.managedObjectContextexecuteFetchRequest:fetchRequesterror:&requestError];/* Make sure we get the array */if([personscount]>0){/* Delete the last person in the array */Person*lastPerson=[personslastObject];[self.managedObjectContextdeleteObject:lastPerson];NSError*savingError=nil;if([self.managedObjectContextsave:&savingError]){NSLog(@"Successfully deleted the last person in the array.");}else{NSLog(@"Failed to ...
Read now
Unlock full access