My app has been under dev for a few months and am now being asked to add Core Data. SMH. It is a tabViewController app. I don’t know if that helps with the answer.
So here I am trying to add it and i’m getting errors. I build for Core Data in the AppDelegate.h, so I have created these IVARs and build them appropriately later in the .m.
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
In the viewControllers that need CoreData interaction, I have given them (in the .h files):
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
and synthesized them in the .m:
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
Now, back to the AppDelegate. In the .m, when I try to pass the pointer for the managedObjectContext from the delegate to the views (like so):
viewController1.managedObjectContext = self.managedObjectContext;
viewController2.managedObjectContext = self.managedObjectContext;
I am getting an XCode Error that reads (for both viewController1 and 2):
Property 'managedObjectContext' not found on object of type 'UIViewController *'
I don’t understand this because the property in both View Controllers clearly exists and is synthesized.
I’ve even tried quitting and restarting XCode. Maybe a system reboot? <– Is that absurd??
(I know I have added more than you may need..) Any help?
I found this answer on another post:
“An alternative approach is to pull the context from your app delegate – at the moment the app delegate has to be re-coded if you change the way your app is organised. You can get a pointer to the app delegate anywhere, so the following piece of code can be added to any view controller:
Usually in the viewDidLoad.”
Thank you to user: jrturton for the answer