My app is just a DB where the user just search some scientifical data. It is build like this:
-It has to “main” views selecteable by a TabBarViewController.
-The first View is where the user just proceeds to the search query.
-The secon view are just some settings, info and disclaimer information.
I used the second view to pre populate the DB which will be shipped along with the app. Now that it’s populated, I’m going to actually implement the settings, info and so on.
The managedObjectContext is set up in the AppDelegate (applicationDidFinish…), like this:
firstViewViewController.managedObjectContext = self.managedObjectContext;
secondViewController.managedObjectContext = self.managedObjectContext;
saveCustom.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
The managedObjectContext is declared ans synthesized properly in the secondViewController.
The App, so far, works OK and I can save items in to the DB.
Now, I created a modalView which is called from the firstViewController. I want the user to be able to save custom data. To do that, I duplicated what I’ve done in the seconViewController and I added the corresponding code (or what I think it is) in the AppDelegate (see above).
When I try to save data using the modalVIew, I’m getting the following error.
* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘+entityForName: could not locate an NSManagedObjectModel for entity name XXX
where XXX is the correct name of my entity.
I know it’s something related to the managedObjectContext not being initialized. But I don’t understand why does it work on the seconViewController and not on the modalViewController.
So, how can I make it work?
I know that importing the AppDelegate to the modalView, although it would work, it’s not a good practise.
Thanks in advance!
Edit:
The code I’m using to save the data is as follows:
-(void)saveDataToCD{
NSString *entityString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:1]];
NSString *deltaString=[NSString stringWithFormat:@"%@",[arrayAdquiritDelMV objectAtIndex:3]];
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *pepe = [NSEntityDescription insertNewObjectForEntityForName:entityString inManagedObjectContext:context]; // aqui un string
[pepe setValue:assigment.text forKey:@"definition"];
[pepe setValue:impurity.text forKey:@"impurity"];
[pepe setValue:[NSNumber numberWithDouble:[delta.text doubleValue]] forKey:deltaString]; //aquí un string
NSError *error;
if (![context save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}}
Edit #2, here is how I call the modal view:
-(IBAction)saveCustom:(id)sender{
if(saveCustomController == nil)
{
SaveCustom *viewTwo = [[SaveCustom alloc] initWithNibName:@"SaveCustom" bundle:[NSBundle mainBundle]];
self.saveCustomController = viewTwo;
[viewTwo release];
}
[self presentModalViewController:self.saveCustomController animated:YES];
}
From what you are describing it don’t look like this,but it still worth a try.
Have you modified your Core-Data model since you’ve launch your application?
If yes you need to go in your application folder and delete the Core-Data backing file.
Because it is now out of sync with your model and core data don’t know what to do with it.
The only doubt I have it’s that I would received that error when I launch my application after a modification, not 2 or 3 views later.
How do you present that view modally, where do you create it and where do you set the context on that view that you present modally?
you are forgetting to tell the UIViewController what context to use.
You are missing a line like this one
When you do it in applicationFinised… your instance don’t exist yet.
And in Obj-C you can send a message to nil, it will just be ignored, you won’t crash like in other language.