NSManagedObjectContext无法删除其他上下文中的对象 [英] An NSManagedObjectContext cannot delete objects in other contexts

查看:588
本文介绍了NSManagedObjectContext无法删除其他上下文中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,每个都显示在自己的UITableView部分。

I have two entities, each displaying on its own UITableView section.

我已启用编辑,允许用户通过向右滑动删除行。这对第一个实体工作正常,但当我尝试删除第二个实体中的对象时,我收到此错误:

I've enabled editing to allow the user to delete rows by swiping to the right. This works fine for the first entity, but when I try to delete an object in the second entity, I get this error:

An NSManagedObjectContext cannot delete objects in other contexts

我得到的错误说,如何适用这里。我使用保留的引用我的上下文来创建,获取和删除数据库中的所有对象,所以我确定只有一个上下文。我也不使用多线程。任何想法可能发生什么?

I get what the error says, but I can't see how it applies here. I use a retained reference to my context to create, fetch, and delete all objects from the database, so I'm sure there's only the one context. I'm also not using multiple threads. Any idea what could be happening?

推荐答案

是从同一实例获取NSManagedObject的上下文使用删除NSManagedObject?如果没有,您需要:

Is the context that you fetched the NSManagedObject from the same instance as the context you're using to delete the NSManagedObject? If not, you need to either:


  • 拥有对同一NSManagedObjectContext的共享引用,以便从同一上下文中删除对象创建或获取它。如果你不使用多线程,那么你应该只需要在代码中调用[[NSManagedObjectContext alloc] init]。


  • 如果必须使用两个不同的NSManagedObjectContext实例,那么从NSManagedObject获取objectID第一个上下文,以便以后可以调用:

  • If you have to use two different instances of NSManagedObjectContext, then get the objectID from the NSManagedObject you got from the first context, so that you can later call:

[context deleteObject:[context objectWithID:aObjectID]];

[context deleteObject:[context objectWithID:aObjectID]];

NSManagedObjectID在上下文之间是相同的,但NSManagedObject本身不是。

The NSManagedObjectID is the same between contexts, but the NSManagedObject itself is not.

这篇关于NSManagedObjectContext无法删除其他上下文中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆