获取NSInternalInconsistencyException错误!

时间:2011-04-18 08:33:21

标签: iphone objective-c

我收到错误:

  

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'+ entityForName:无法找到实体名称的NSManagedObjectModel'练习''

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

(您可以在此处查看我的数据模型:How is This Data Model?)。 有什么想法吗?

   - (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];
    }

1 个答案:

答案 0 :(得分:0)

此错误只有几个可能的来源:

  1. 实体名称中的错字。
  2. 无管理对象上下文对象。
  3. 未能将包含实体的模型添加到上下文使用的持久性存储中。
  4. 未能将正确的持久存储添加到上下文本身。
  5. 请参阅上一个问题:

    insertNewObjectForEntityForName:

    <强>更新

    为什么if语句被注释掉了?

    if(managedObjectContext == nil) 
    

    我认为这是必需的。

    修改

    -(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];
        }
    }