Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a ModalView to save data via CoreData
    text
    copied!<p>My app is just a DB where the user just search some scientifical data. It is build like this:</p> <p>-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.</p> <p>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.</p> <p>The managedObjectContext is set up in the AppDelegate (applicationDidFinish...), like this:</p> <pre><code> firstViewViewController.managedObjectContext = self.managedObjectContext; secondViewController.managedObjectContext = self.managedObjectContext; saveCustom.managedObjectContext = self.managedObjectContext; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; </code></pre> <p>The managedObjectContext is declared ans synthesized properly in the secondViewController.</p> <p>The App, so far, works OK and I can save items in to the DB.</p> <p>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).</p> <p>When I try to save data using the modalVIew, I'm getting the following error.</p> <p><em><strong></em>* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name XXX</strong></p> <p>where XXX is the correct name of my entity.</p> <p>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.</p> <p>So, how can I make it work?</p> <p>I know that importing the AppDelegate to the modalView, although it would work, it's not a good practise.</p> <p>Thanks in advance!</p> <p>Edit:</p> <p>The code I'm using to save the data is as follows:</p> <pre><code>-(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:&amp;error]) { NSLog(@"Problem saving: %@", [error localizedDescription]); }} </code></pre> <p>Edit #2, here is how I call the modal view:</p> <pre><code>-(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]; </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload