Let's get started with our code examples:
For our code, we will first declare some Core Data objects in our
AppDelegate
class inside ourAppDelegate.h
file such as:@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
These are declared here so that we can access them easily from any screen.
Next, we will declare the code for each of the objects in
AppDelegate.m
such as the following lines of code that will create an instance ofNSManagedObjectContext
and return an existing instance if the instance already exists. This is important as you want only one instance of the context to be present to avoid conflicting access to the context:- (NSManagedObjectContext *)managedObjectContext { if (_managedObjectContext != nil) { return _managedObjectContext; ...