Search code examples
core-dataxcode7nsindexpath

Remove entry from Core Data if indexPath is known


I'm trying to remove a specific entry on my UITableView. I know the specific indexpath of that entry but I need to delete that entry first in core data. but I could not. this is the code that I am using:

AppDelegate * appDelegateMaster = (AppDelegate*)[[UIApplication sharedApplication] delegate];
self.manageObjectContext = appDelegateMaster.managedObjectContext;
                         NSManagedObjectContext *contextObj = self.manageObjectContext;
Messages * entryToDelete = [self.fetchResultController objectAtIndexPath:indexPathToRefresh];
[contextObj deleteObject:entryToDelete]; // delete the entry
NSError *error = nil;
if (![contextObj save:&error]) {}

I'm deleting this programmatically so commitEditingStyle is not an issue. this is being done outside tableview's Delegates.


Solution

  • I'd recommend deleting data in the model and then using the callback/delegate methods to run an update on the UI.

    Specifically, the NSFetchedResultsController does handle this kind of scenario. If you want to do this manually, then delete the object and reload the tableView (entirely or just the indexpath/section that has been changed)