10

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?

2
  • I guess you're disconnecting a relationship while processing the JSON, a delete rule is removing something and you don't update the UI fully?!?
    – Wain
    Commented Jul 2, 2016 at 19:48
  • @Wain please see the question update
    – Can
    Commented Jul 4, 2016 at 16:44

2 Answers 2

12

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.

1
  • I was playing around with CoreData and I inserted an invalid Object, and then attempted a save and rollback. The save failed and the rollback removed this inserted Object from the context. After that I tried setting attributes of this Object and received similar error. Commented Feb 6, 2020 at 12:16
1

This can happen if you call reset on an NSManagedObjectContext, and try to use an object inserted into that context before you called reset.

let context = cdq.mainContext

let u = User.init(context: context)
u.name = "foo"

context.reset()

u.name = "bar"

This will cause a CoreData: error: Mutating a managed object 0xc9891a8bcbb85cb7 <x-coredata://505A879D-EC14-4A3E-8B6B-55BDD14DDC5D/Message/p260> (0x7b140009ee80) after it has been removed from its context. error.

This will not necessarily crash the app. The error will be logged to the console either way.

Doing this can also cause an EXC_BAD_INSTRUCTION crash like this:

#0  0x00007fff2516b03e in -[NSManagedObjectContext _forceRegisterLostFault:] ()
#1  0x00007fff250b81bf in _PFFaultHandlerLookupRow ()
#2  0x00007fff250c1faf in _PF_ManagedObject_WillChangeValueForKeyIndex ()
#3  0x00007fff250c39a7 in _sharedIMPL_setvfk_core ()
#4  0x00007fff250dc290 in _sharedIMPL_copying_setvfk_core ()

From Apple's docs on NSManagedObjectContext.reset()

All the receiver's managed objects are “forgotten.” If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.