Search code examples
iosnsfetchedresultscontroller

Passing NSFetchedResultsController between UIViewControllers


I have 3 different view controllers that present same data. Basically they are presented one after another. They are presenting same entity that is fetched with NSFetchedResultsController.

I am now pondering since these are same NSFetchedResultsControllers, should I just optimize things and create one, that I pass between controllers? Is there any downside to this, or even better is this good practice?

Also just passing dataset wouldn't suffice since i want controllers to react to changes to database.


Solution

  • should I just optimize things and create one, that I pass between controllers?

    Yes, minimising the overhead of performing a similar fetchRequest on init of every VC is a good practice.
    A better alternate instead of passing the NSFetchedResultsControllers object between VCs would be -

    • To completely abstract out all FetchedResultsControllers initialisation & delegate function implementations to a helper class christened like:
      @interface CoreDataHelper: NSObject < NSFetchedResultsControllerDelegate >

    • You'll need re-wiring of the delegate listeners & choose appropriate time for allocating & if required(not essential) deallocating of the CoreDataHelper instance

    • A singleton CoreDataHelper will serve you well enough to handle all Core Data interactions.

    Segregating Model interactions away & independent from View components enables for creating loosely-coupled modules, making them reusable & succinct.