Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data fetch request fails with: 'NSInternalInconsistencyException', reason: 'statement is still active'

Basic app layout using the navigation controller template with core date:

Delegate: persistenceCoordinator, managedObjectModel, managedObjectContext

  • RootView: managedObjectContext (from delegate), addManagedObjectContext (using the pattern from the templates), fetchResultsController
    • Add/Edit view (a hierarchy for adding Object A & its B)

I have two objects, A & B that share a 1-to-1 relationship/inverse relationship

Using the pattern from the templates the app:

  1. Creates an addManagedObjectContext
  2. Inserts an A entity
  3. Pushes on the Add/Edit A view (having passed it the inserted entity)

EXCEPTION STEPS 1:

If I simply fill in the A attributes and save everything works fine.

  • The object is inserted into the database (verified with sqlite3 on the command line)
  • The fetchResultsController updates the list view
  • I can close and open the app (full close/open not just app switch) and the list view will re fetch correctly

If I then Edit A, I can select Add B and an add B view is pushed on (and passed the A entity)

  • Fill in B details and save
  • B is inserted into A's managedObjectContext, the relationship is set, and the context is saved
  • The view pops off and all appears well
  • The objects are both in the database with the correct relationship
  • I can reload the record and see the relationship, make edits etc
  • EXCEPTION: If I close/reopen the app, so as to trigger a fetch, then the error mentioned in the title will occur.

EXCEPTION STEPS 2:

If I fill in the A attributes AND the B attributes in the initial "add"

  • Saving B triggers a full save of A's context (as above)
  • Saving A saves via the delegate relationship set up by the template (and merges the result into the main managedObjectContext etc)
  • The objects are inserted into the database correctly
  • However the fetchResultsController DOES NOT update to show the new records
  • EXCEPTION: If I close/reopen the app, so as to trigger a fetch, then the error mentioned in the title will occur.

I've tried saving A first, adding a separate context for B etc etc. I'm confused to say the least. Any help would be greatly appreciated.

Cheers, Ryan

like image 391
Ryan Booker Avatar asked Jun 20 '10 04:06

Ryan Booker


1 Answers

It sounds like you have two or more managed object context when you just need one.

I don't know why you would have a method called addManagedObjectContext in the RootViewController or what it would do. In all but the most complex designs, you have only one context at a time and they don't overlap. The context/s should be held by the app delegate or a dedicated model object. You would never create a new context every time you open a particular view.

You can have different context all writing to the same persistent store so I think this is why you are seeing the data in the store. However, you can't maintain graph integrity across context if you put one object in a relationship in one context and the other side of the relationship in another context. I believe this is what is producing your error.

like image 136
TechZen Avatar answered Sep 30 '22 12:09

TechZen