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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With