简体   繁体   中英

CoreData: error: Mutating a managed object after it has been removed from its context

I built a simple app retrieving some JSON and storing the data inside 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.

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?

Update

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?

The error is interesting:

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

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 save s or operate in a child context which you throw away before each new fetch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM