When we create core data object automatically I see a line
@synthesize managedObjectContext=__managedObjectContext;
However, I do not think we will ever need that line because the code also generate a function
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
So what am I missing here?
Why synthesize a code that we DO write?
The
@synthesizedirectiveYou’re still allowed to create your own implementations.
So why use
@synthesize? To associate the variable (storage) called__managedObjectContextwith the property (public access point) calledmanagedObjectContext.Why do this? As Henrik noted, so that you can do lazy setup of the storage.