CoreData:错误:在托管对象从其上下文中删除后对其进行变更 [英] CoreData: error: Mutating a managed object after it has been removed from its context

查看:129
本文介绍了CoreData:错误:在托管对象从其上下文中删除后对其进行变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个简单的应用程序,检索一些JSON并将数据存储在Core Data中。

I built a simple app retrieving some JSON and storing the data inside Core Data.

在模拟器或设备上进行初始安装时,所有Core Data操作都是很好,但重新运行时,我收到以下错误消息:

Upon the initial install on the simulator or a device, all Core Data operations are fine but upon re-runs, I get the following error message(s):

2016-07-02 13:23:53.925 En Yakın[84775:5379467] CoreData: error: Mutating a managed object 0x79736290 <x-coredata:///Category/t4B10F995-A717-4DB8-9E87-8A1C079D45D42> (0x79736250) after it has been removed from its context.

视觉上没有任何错误。所有数据都按预期显示并且应用程序功能。

There is nothing wrong visually. All data is presented as expected and the app functions.

我调试了问题。如果我注释掉JSON检索功能并使应用程序在初始运行后使用Core Data中的内容,则不会显示任何错误消息。但是再次检索应用程序会导致与我的核心数据堆栈(作为单个对象实现)的错误传达。

I debugged the problem. If I comment out the JSON retrieval function and make the app use what's inside Core Data after the initial run, no error messages are represented. But making the app retrieve again causes a miscommunication with my Core Data Stack(implemented as a singleton object).

我应该如何修改我的核心数据实现?

How should I modify my Core Data implementation?

更新

我相信我已经将问题跟踪到它的核心。我正在存储我下载的大约6 KB的缩略图。我发现存储图像会给Core Data带来性能损失。但是这种尺寸的缩略图也会有问题吗?如果我删除了对实体的图像分配,则错误消失。我应该将它们存储在文件结构中吗?

I believe I tracked the problem to it's core. I'm storing thumbnail images of approximately 6 KB I've downloaded. I found out that storing images will put a performance hit to Core Data. But will thumbnails of this size is problematic too? If I remove the image assignment to the entity, the errors disappear. Should I store them inside the file structure?

推荐答案

错误很有趣:

< x-coredata:/// Category / t4B10F995-A717-4DB8-9E87-8A1C079D45D42>

请注意GUID前面的小写 t 。这意味着此对象是新的,尚未保存。因此,除非您在每次使用后丢弃未保存的托管对象上下文,这是一个完全有效的策略,否则您的状态不匹配。

Note the lower case t in front of the GUID. That means this object is new and has not been saved. Hence, unless you are throwing the unsaved managed object context away after each use, a fully valid strategy, you have a state mismatch.

解决问题的简单方法是要么发出更多的保存,要么在每次新获取之前丢弃的子上下文中进行操作。

The easy way to solve your problem is to either issue more saves or operate in a child context which you throw away before each new fetch.

这篇关于CoreData:错误:在托管对象从其上下文中删除后对其进行变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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