What is the best way to pass NSManagedObjectContext around in iOS applications?

TL;DR Passing an NSManagedObjectContext in each and every NSViewController that needs CoreData can be done by different means, but some are frowned upon. In doubt, inject it. While pair-reviewing some code with my colleague Vincent Tourraine, we realized we wrote this kind of code into each viewDidLoad: method of each NSViewController subclasses we created: managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext]; Doing so is more or less the same as using a global variable, as calling sharedApplication on UIApplication returns the singleton application instance. I kind of felt this was a code smell so I investigated. ...

January 18, 2012 · 2 min · Franck Verrot