October 2013
Intermediate to advanced
1053 pages
28h 7m
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]initWithEntityName:@"Person"];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 delete the last person in the array.");}}else{NSLog(@"Could not find any Person entities in the context.");}self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];returnYES;}
In this example code, we are using the createNewPersonWithFirstName:lastName:age: ...
Read now
Unlock full access