Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
Can Avatar asked Jul 02 '16 11:07

Can


People also ask

What is context in Coredata?

From your perspective, the context is the central object in the Core Data stack. It's the object you use to create and fetch managed objects, and to manage undo and redo operations. Within a given context, there is at most one managed object to represent any given record in a persistent store.

Can we use Core Data managed objects from background thread?

Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.

What is Coredata stack?

Core Data provides a set of classes that collaboratively support your app's model layer: An instance of NSManagedObjectModel describes your app's types, including their properties and relationships. An instance of NSManagedObjectContext tracks changes to instances of your app's types.


1 Answers

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

like image 66
adonoho Avatar answered Oct 23 '22 18:10

adonoho