2011-04-18 2 views
0

Je reçois l'erreur:Obtention d'une erreur NSInternalInconsistencyException!

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

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

(Vous pouvez voir mon modèle de données ici: How is This Data Model?). Des idées pourquoi?

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

     [email protected]"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]; 
    } 
+0

Voir ma réponse à jour – visakh7

Répondre

0

Cette erreur n'a plus que quelques sources possibles:

  1. Typo au nom d'entité.
  2. Objet de contexte d'objet géré nul.
  3. Echec de l'ajout du modèle contenant l'entité au magasin persistant utilisé par le contexte.
  4. Impossible d'ajouter le magasin persistant correct au contexte lui-même.

S'il vous plaît se référer également à cette question précédente:

insertNewObjectForEntityForName:

MISE À JOUR

Pourquoi la déclaration if en commentaire?

if(managedObjectContext == nil) 

Je pense que c'est nécessaire.

EDIT

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

     [email protected]"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]; 
    } 
} 
+0

je commentais dehors parce que quand il n'a pas été commenté, elle ne tenait pas créer l'objet géré, je sais cela parce que le NSLog je n'ai l'impression. –

+0

Je ne sais pas. Je les ai juste laissés au cas où je décommentais la déclaration if –

+0

Mais l'erreur arrive comme SIGABRT à la ligne indiquée dans la question. –

Questions connexes