Search code examples
objective-ccore-datansmanagedobjectcontext

How to stop Core Data application from loading managedObjectContext automatically?


I am building a core data application that checks for a saved user login upon launch and sets up the model, store, coordinator and context afterwards. The only problem I have is that as soon as the user clicks on any view in the interface, the application tries to get the managedObjectContext causing an exception seeing as I haven't created the stores yet.

Is there any way to stop it from doing this?

Cheers.


Solution

  • If you're using the Coredata boiler plate stuff provided by Apple, you will notice the managedObjectContext object is loaded lazily when its property is accessed.

    Simply tell your view controller to access the context via its property (i.e. self.managedObjectContext) instead of accessing the variable directly, and the context, object model and persistent store coordinator will be created appropriately.

    PS: This is just a guess as you didn't post any of your related code here.