0

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];
}
Wain
  • 118,658
  • 15
  • 128
  • 151
lycros
  • 193
  • 2
  • 12

2 Answers2

1

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).'

lycros
  • 193
  • 2
  • 12
streem
  • 9,044
  • 5
  • 30
  • 41
  • 1
    You shouldn't add another problem in an answer, it won't help somebody with the same problem, and could be confusing. It is better to add a comment. Your new error has nothing to do with CoreData, it says that if you delete a row, the new total should match the number given in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section – streem Mar 12 '14 at 18:44
  • I'm sorry, you're right the error is not the elimination of the data in the core data, the error is not cleared in the tableview, I'm using [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic], but does not work – lycros Mar 12 '14 at 22:45
  • You can check that link http://stackoverflow.com/questions/5043729/core-data-example-invalid-update-invalid-number-of-rows-in-section-0 It is about inserting a new row but it is the same issue. – streem Mar 13 '14 at 08:31
0

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)

Wain
  • 118,658
  • 15
  • 128
  • 151
  • ok, yes it is, but the error is the index of the row of tableview i update my question – lycros Mar 12 '14 at 17:44
  • Forgot the save. Debug, is the object deleted? What is the save error? – Wain Mar 12 '14 at 17:51
  • the error is not the elimination of the data in the core data, the error is not cleared in the tableview, I'm using [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic], but does not work – lycros Mar 12 '14 at 22:46
  • The error is that whatever you're using to populate the table (provide the row count) isn't having an item removed. My answer is now removing an item from your array and Core Data. That should cover the bases. What do you use the return the row count? – Wain Mar 12 '14 at 22:47
  • return [self.fetchedRecordsArray count]; – lycros Mar 12 '14 at 23:10