Weston
Weston

Reputation: 1491

NSFetchedResultsController doesn't fetch up the child-parent moc chain?

I cannot find any clarification on this, so it may be a bug.

Problem is, I have a series of parent-child Managed Object Context's.

When I save on a child context the changes get pushed up to the parent, and I can fetch using a plain old NSFetchRequest.

However, if I rely on an NSFetchedResultsController to pull these changes into a sibling context to the first, they do not see them.

calling -(void)performFetch: error; doesn't seem to pull the changes either.

After a restart of the app, all new data is available.

My hypothesis is that NSFetchedResultsController only fetches from its current context and will not follow the chain to the persistent store.

Can someone please set me straight here?

Am I going to have to use notifications to monitor changes on other contexts?

and finally, is this mentioned somewhere in the doc's? I cannot find it for the life of me.

Upvotes: 2

Views: 1031

Answers (1)

XJones
XJones

Reputation: 21967

When you save a child context the changes are propagated to the parent but they are not committed to the persistent store until the root context (i.e. no parent) is saved. Changes in a parent context are not pushed to other children (siblings).

The way I handled this is I configured my root context to be NSMainQueueConcurrencyType and I use the root context for my fetched results controller. If you prefer to use a child context for your FRC then after the parent saves, reset the child context and perform the fetch again.

Apple Docs

The relevant para from the docs is:

When you save changes in a context, the changes are only committed “one store up.” If you save a child context, changes are pushed to its parent. These changes are not saved to the persistent store until the root context is saved. (A root managed object context is one whose parent is nil.) In addition, a parent does not pull changes from children before it saves. You must save a child contexts if you want ultimately to commit the changes.

The full doc is at Core Data Release Notes for OS X v10.7 and iOS 5.0

Upvotes: 6

Related Questions