Ios NSManagedObjectContext无法删除其他上下文中的对象
错误是NSManagedObjectContext无法删除其他上下文中的对象用于获取NSManagedObjectContext的NSManagedObjectContext实例与当前实例不同Ios NSManagedObjectContext无法删除其他上下文中的对象,ios,objective-c,Ios,Objective C,错误是NSManagedObjectContext无法删除其他上下文中的对象用于获取NSManagedObjectContext的NSManagedObjectContext实例与当前实例不同 也请检查此项:您必须使用与调用[self-managedObjectContext]返回的NSManagedbjectContext不同的NSManagedbjectContext来获取与self.devices关联的数组中的对象。无法使用其他上下文更改或操作您试图删除的对象,这就是为什么会出现错误的原因
也请检查此项:您必须使用与调用
[self-managedObjectContext]
返回的NSManagedbjectContext不同的NSManagedbjectContext来获取与self.devices
关联的数组中的对象。无法使用其他上下文更改或操作您试图删除的对象,这就是为什么会出现错误的原因
如果要删除该对象,可以尝试将其拉出,然后按如下方式删除/更改该对象:
- (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];
}
}
这在您的情况下是可行的,但是您真正应该做的是使用NSFetchedResultsController使代码更易于使用。以下是一些关于如何使用它的有用链接:
您有自助设备。它是核心数据对象的数组。在某些方法中,可以使用NSManagedObjectContext获取此数组……但在上面的代码中,删除时使用了NSManagedObjectContext的另一个实例。感觉通过方法使用自己的核心数据对象数组犯了一个很大的错误,这是不安全的。最好使用NSFetchedResultsController进行同步。先生,我可以向您发送我的应用程序吗?请解决问题。请siryep。我会尽力解决的。可能是
- (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];
}
}