Reputation: 193
I can not find how to delete a core data entity instance from my table view.
This is how I update the table view with information from core data:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ ",record.punto];
UIImage* image = [UIImage imageWithData:record.imagen];
cell.imageView.image = image;
return cell;
}
and I am trying to use the following function to delete the entity instance, but it isn't working:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Upvotes: 0
Views: 1207
Reputation: 119041
You need to delete the entity instance from the managed object context:
NSManagedObjectContext *moc = record.managedObjectContext;
Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
[self.fetchedRecordsArray removeObject:record];
[moc deleteObject:record];
NSError *error;
if (![moc save:&error]) {
NSLog(@"Save error:: %@", error);
}
this goes in tableView:commitEditingStyle:forRowAtIndexPath:
(Assuming that Datos
is your managed object subclass)
Upvotes: 0
Reputation: 9154
Well, i'm not sure where you get your data from your CoreData but you need to call a that kind of snippet, it won't delete by itself when deleting row
- (void) deleteElement:(NSString*)elementId{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"YourTable" inManagedObjectContext:self.managedObjectContext]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"id=%@",elementId]];
NSError *error=nil;
NSManagedObject *fetchedElement = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];
[self.managedObjectContext deleteObject:fetchedElement];
if (![self.managedObjectContext save:&error]) {
NSLog(@"problem deleting item in coredata : %@",[error localizedDescription]);
}
}
And your code should look like :
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self deleteElement:idOfElementToDelete];
}
this is the error:
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Upvotes: 1