Getting an NSInternalInconsistencyException Error!
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];
}
-(IBAc开发者_开发问答tion)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];
}
This error has only a few possible sources:
- Typo in the Entity name.
- Nil managed object context object.
- Failure to add the model containing the entity to the persistent store the context uses.
- 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];
}
}
精彩评论