Search code examples
iosobjective-ccore-datanspredicatensfetchedresultscontroller

Dynamically change fetchedResultsController


I am stuck with an issue: in my project I'm using Core Data to manage data. I've got a single fetchResultsController used in all the project.

In AppDelegate.m I perform a fetch request and everything is ok. The data is shown in 2 tableViews: AllRecipes and FavouriteRecipes. I need to somehow get all the recipes in AllRecipesViewController and only favourite recipes FavouriteRecipesViewController.

I set a predicate to get only favourite recipes in the FavouriteRecipesViewController viewDidAppear method and a predicate to get all the recipes in AllRecipesViewController. But I'm not sure that this is ok to do.

Can anyone tell me a better way to do it?


Solution

  • You can do two things:

    1. Have a separate NSFetchedResultsController for each view and do the fetching when the new UIViewController is instantiated (what I would do).
    2. Update the predicate on the NSFetchedResultsController and re-fetch each time you go to a new UIViewController (what I would not do).