Question regarding Core Data, parent/child contexts, and private/main queues?
Question regarding Core Data, parent/child contexts, and private/main queues?
- Subject: Question regarding Core Data, parent/child contexts, and private/main queues?
- From: Michael Crawford <email@hidden>
- Date: Sun, 11 Dec 2011 19:52:42 -0500
I'm using a parent context on a private queue and a child (main) context on the main queue. I do everything in the UI with the child context and save the results as the view disappears.
I had a bug where the first time I ran the app, I was able to add objects and save them in the child context. When the app would terminate or enter the background, I would save the child context (because that is the way my old code was setup). The next time I ran the app, it would crash while loading the initial table view from a fetched-results-controller (FRC). The crash was due to getting an object from the controller using -objectWithIndex:. The FRC says it has one object but when I go to access it, I get an exception due to an invalid index (no data)!
As soon as I replaced the final save of the child context, during transition to background or termination, with a save of the parent context, this problem went away. My theory is that the save to the child context updates the indexes but the data never actually hits the disk since I never executed a save on the master context.
I think I have another problem, though I haven't had it blow up yet, and that is that I'm executing the save of the parent context from my main thread when the app is transitioning to the background or terminating. Remembering that this parent context is associated with a private queue I wonder if this is ok? I seem to recall Marcus Zarra saying everything being executed on a private queue MOC has to be executed using the -performBlock: or -performBlockAndWait: calls.
-Michael
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden