user4091658
user4091658

Reputation:

An NSManagedObjectContext cannot delete objects in other contexts

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"%@",context);
        // Delete object from database
        [context deleteObject:[self.devices objectAtIndex:indexPath.row]];
        NSLog(@"%@",context);

    NSError *error = nil;
    if (![context save:&error]) {enter code here
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    }

    // Remove device from table view
    [self.devices removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


   }
}

the error is An NSManagedObjectContext cannot delete objects in other contexts

Upvotes: 1

Views: 2418

Answers (2)

sashimiblade
sashimiblade

Reputation: 620

You must have fetched the objects in the array associated with self.devices with a different NSManagedObjectContext than the one that is returned by calling [self managedObjectContext]. The object you are trying to delete cannot be changed or operated using a different context, which is why you are getting the error.

What you can try to do, if you want to delete that object, is pull it out and then delete/change the object like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context;

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"%@",context);
        // Delete object from database
        MyObject *object = [self.devices objectAtIndex:indexPath.row];
        context = object.managedObjectContext;
        [context deleteObject:object]
        NSLog(@"%@",context);

    NSError *error = nil;
    if (![context save:&error]) {enter code here
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    }

    // Remove device from table view
    [self.devices removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


   }
}

This will work in your situation, but what you should really do to make your code much easier to work with is use an NSFetchedResultsController. Here are some useful links about how to use it:

http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/index.html

Upvotes: 0

kabarga
kabarga

Reputation: 803

The instance of the NSManagedObjectContext that you are using to fetch NSManagedObject is different than the current one.

Check this also: An NSManagedObjectContext cannot delete objects in other contexts

Upvotes: 1

Related Questions