objective c - Delete from UITableView using an NSFetchedResultsController -


i have uitableview populated through nsfetchedresultscontroller. when user slides item right have delete button appearing he/she can remove object using following approach:

// override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable.     return yes; }   - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath {     return yes; }    - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {  if (editingstyle == uitableviewcelleditingstyledelete) {     //perform similar delete action above 1 cell      xmppusercoredatastorageobject *user = [[self fetchedresultscontroller] objectatindexpath:indexpath];      nslog(@"user delete: %@", [user displayname]);      //delete fetchcontroller     nsarray *sections = [[self fetchedresultscontroller] sections];     int userstatus = [[user sectionnum] intvalue];       [tableview deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];      } } 

but when there exception because not updating model fetchedresultscontroller. question how remove fetchedcontroller using indexpath have commiteditingstyle ?

since using nsfetchedresultscontroller need delete object context, , pick change , delete rows you.

so remove deleterowsatindexpaths: line , call instead:

[self.fetchedresultscontroller.managedobjectcontext deleteobject:user]; 

Comments