The problem here is that your accessor and your ivar have the same name. That's where the underbar ivar convention comes from. Here, you're not using the accessor to access your property, you're using the backing variable directly, so it never gets initialize. Instead, make sure you always go through your accessor methods and you won't have a problem. So, rewrite the offending method (and any others that use the managedContextObject
property with something like the following:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; // it's good practice to call the super methods, even if you're fairly certain they do nothing
// Get a reference to the managed object context *through* the accessor
NSManagedObjectContext* context = [self managedObjectContext];
// From now on, we only use this reference in this method
NSFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Remind" inManagedObjectContext:context]; // <- use the local reference we got through the accessor
[request setEntity:entity];
NSError* error = nil;
NSArray* array = [context executeFetchRequest:request error:&error];
if( !array ) {
// Do something with the error
NSLog(@"Error Fetching: %@", error);
}
[self setDesitnationsArray:[array mutableCopy]];
[destinationsTableView reloadData];
}
You might want to change your ivars to something you won't be tempted to use or that will be immediately apparent that you haven't gone through the accessors, like _managedObjectContext
or even _privateContext
or whatever will stick out to you until you get used to accessing properties through the accessors. If you don't like the Objective-C syntax for accessing properties, you could use the dot syntax, but you must always remember to go through self
, for example, self.managedObjectContext
. I don't like this method as people forget that it's not a direct property access and it is using the accessors, so they think it's okay to interchange the dot syntax for a direct access, when it's not (like in your case).