5
votes

I am working with Core Data and trying to get it to display data with a simple data model. The app crashes and gives me this error message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Remind''

I am not totally sure but how I take it is that it is saying that it can't find my entity called Remind? However, I do in fact have an entity called Remind.

I also put breakpoints and it stops right here: enter image description here

Any help would be greatly appreciated. Completely at a dead end.

Managed Context code in App Delegate .m

enter image description here

1
Your managed object context is nil. I'm assuming it's an ivar... It hasn't been set to anything.Jason Coco
Thanks for your response. Can you tell me how I might be able to fix this issue?Zack
Unfortunately no, or I would have put it in an answer. You need to add more code, there's not enough information here. Where in your view controller are you setting the managedObjectContext ivar?Jason Coco
Well in all honesty I am using source code. And the source code works just fine. it just doesn't seem to want to work with mine. And I am setting it up in the App Delegate. Ill edit the post to show you my app delegate .m file where I am setting it up. It must be something very simple though.Zack

1 Answers

1
votes

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).