Search code examples
ioscore-datansmanagedobjectcontextnsfetchrequest

Core Data: Fetch objects from multiple ManagedObjectContexts


My iPad App is using throw-away child ManagedObjectContexts for editable ViewControllers, so I can easily discard the edited data. Now I want to show a list of all objects in an overlay view, including the unsaved objects that may still being edited in one of these child MOCs.

Is there a way to get all existing MOCs to make a fetch request across all contexts? I know that I could keep track of all the contexts I create, but I'm curious if there is a more elegant solution.


Solution

  • No, only the managed object context in which the changes were made knows about them until you save the context and with this trigger the change propagation.

    An alternative to using child MOC as "scratch pads" is to use the undo manager. This way the changes you make will be all in the same MOC (so a fetch request would see them all), but you would still have the ability to roll them back.

    This might become a little bit hairy though if you really need multiple independent "scratch pads" at the same time. However, in this case I would also have a look at the interaction model of the app, since having multiple independent pending changes at once seems quite complex for the constraints of an iPad interface.