0

I am getting the error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Exercise''

At the line: Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

(You can see my data model here: How is This Data Model?). Any ideas why?

   - (void)viewDidLoad
    {
        [super viewDidLoad];

        UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(exerciseChooser)];
        self.navigationItem.rightBarButtonItem = addButton;
        [addButton release];

        //if (managedObjectContext == nil) 
        { 
            managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        }

        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
        [request setEntity:entity];

        NSLog(@"After managedObjectContext: %@",  managedObjectContext);


        NSError *error = nil;
        NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
        if (mutableFetchResults == nil) {
            // Handle the error.
        }
        [mutableFetchResults release];
        [request release];
    }

    -(IBAction)exerciseChooser
    {
        RoutineExerciseChooserViewController *routineExerciseChooserViewController = [[[RoutineExerciseChooserViewController alloc] init] autorelease];

        [self.navigationController pushViewController:routineExerciseChooserViewController animated:YES];
    }

    -(void)addExercise
    {    
        Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

        exercise.name=@"Test";

        NSError *error = nil;
        if (![managedObjectContext save:&error]) 
        {
            // Handle the error.
        }
        NSLog(@"%@", error);

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        NSInteger lastSection = [self.tableView numberOfSections] -1;

        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:lastSection]-1 inSection:lastSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
Community
  • 1
  • 1

1 Answers1

0

This error has only a few possible sources:

  1. Typo in the Entity name.
  2. Nil managed object context object.
  3. Failure to add the model containing the entity to the persistent store the context uses.
  4. Failure to add the correct persistent store to the context itself.

Please also refer to this previous question:

insertNewObjectForEntityForName:

UPDATE

Why is the if statement commented out?

if(managedObjectContext == nil) 

I think it's required.

EDIT

-(void)addExercise
{    
    if(managedObjectContext!=nil)
    {
        Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

        exercise.name=@"Test";

        NSError *error = nil;
        if (![managedObjectContext save:&error]) 
        {
            // Handle the error.
        }
        NSLog(@"%@", error);

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        NSInteger lastSection = [self.tableView numberOfSections] -1;

        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:lastSection]-1 inSection:lastSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}
Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • I commented it out because when it was not commented, it wouldnt not create the managed object, i know this because the NSLOG i have would not print. –  Apr 18 '11 at 08:49
  • I don't. I just left them in incase I uncommented the if statement –  Apr 18 '11 at 08:53
  • But the error is coming as SIGABRT at the line indicated in the question. –  Apr 18 '11 at 08:54
  • Did u check if the objectmodel exists? check it before adding new item – visakh7 Apr 18 '11 at 08:55
  • I think its because your objectmodel is nil. – visakh7 Apr 18 '11 at 08:56
  • No, I did not. How can I do this? Thanks. –  Apr 18 '11 at 08:56
  • I have already NSLogged the managedObjectContext in the code if that is what you are referring to. And this is what I get: ``. –  Apr 18 '11 at 09:01
  • Did u check before adding new exercise – visakh7 Apr 18 '11 at 09:05
  • Yes, this is all before adding a new exercise. I can't even check after because it crashes before it could add. –  Apr 18 '11 at 09:06
  • Debug and check if your control goes to method addExercise and check whats causing the issue – visakh7 Apr 18 '11 at 09:08
  • Well, I have a 3rd view, that when you select a row, in the didSelectRow method, it calls the method addExercise in a different class 2. this class 2 is the one I am dealing with. So I know the method is being called because the error line is in the class that the method to be called is in. –  Apr 18 '11 at 09:09
  • I am currently checking the model before the method gets called -- Lets say this is class 1. There is another view, class 2, that calls this addExercise method in class 1. Class 1 is the one with all the NSfetchedresults and managed object code in it, Class 2 does not have any of this code in it because it is not doing any core data stuff itself, rather it is calling the method from class 1. –  Apr 18 '11 at 09:15
  • I think when you call back this method the context is not there somehow. I am not sure but can you check its value. I think its released. Hence the crash – visakh7 Apr 18 '11 at 09:18
  • I think I have an idea, when I call the addExercise method from the other class, it is not loading the view, so the viewDidLoad is not initializing the mangedObjectContext. You think that may be it? What is a solution to this. –  Apr 18 '11 at 09:18
  • Yes exactly there is the problem. Try initializing it viewWillAppear. – visakh7 Apr 18 '11 at 09:20
  • Ok, great, so it doesn't crash anymore so the method is going through, however my table is not updating with the new cell, so let me check that out. –  Apr 18 '11 at 09:23
  • Yes so the context is nil thats why it crashes. So create the context here again or do it in viewWillAppear: – visakh7 Apr 18 '11 at 09:24
  • Is it ok to just create the objectcontext in my addExercise method? –  Apr 18 '11 at 09:27
  • I think its okay. You can try in viewWillAppear I think its better – visakh7 Apr 18 '11 at 09:29
  • you are not creating a new one here I think you are using the one that you created in appDelegate so I guess its not a problem – visakh7 Apr 18 '11 at 09:29
  • Ok cool. For some reason the cell isn't being updated until after I quit the app and launch it again. Only then do I see the cell called "test" that was supposed to be added. –  Apr 18 '11 at 09:32
  • Call [self.tableView reloadData]; at the end of the method – visakh7 Apr 18 '11 at 09:33
  • There is some problem still lingering I think but its probably related to the cell method. Its 6 am here so I think its time to sleep Ill figure out the rest tomorrow. Thanks! –  Apr 18 '11 at 09:37
  • Thanks I will. One more thing, Any idea why I get this error? `*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'The NSManagedObject with ID:0x12c130 has been invalidated.'` –  Apr 18 '11 at 09:40
  • If the object has been invalidated it usually means the underlying managed object model has been released. You need to make sure you retain the managed object model that contains the objects you are dealing with. – visakh7 Apr 18 '11 at 09:44
  • Ok, how can i fix that, im getting that error when I click on another cell in the table. its always the 2nd cell i click, doesnt matter which one, that crashes and gets that error. –  Apr 18 '11 at 09:45
  • http://stackoverflow.com/questions/2592333/iphone-development-coredata-runtime-error – visakh7 Apr 18 '11 at 09:45
  • You have to check your code and see the use of the model, make sure they exist when u need them and are not released or auto-released. – visakh7 Apr 18 '11 at 09:47
  • Is it because I do use self.managedObjectContext = nil;in viewdidunload and release the managedobjectcontext in dealloc? –  Apr 18 '11 at 09:49
  • ahh its because i was releasing fetchedresults in dealloc, i guess you're not supposed to do that. –  Apr 18 '11 at 09:52
  • hm yes you dont take ownership of it so dont release it – visakh7 Apr 18 '11 at 09:53