Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Giving Error

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

like image 719
Zack Avatar asked Nov 12 '22 20:11

Zack


1 Answers

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

like image 84
Jason Coco Avatar answered Nov 15 '22 10:11

Jason Coco