Error "'+entityForName: could not locate an NSManagedObjectModel"

Discussion in 'iOS Programming' started by mikezang, Apr 26, 2012.

  1. mikezang, Apr 26, 2012
    Last edited: Apr 26, 2012

    mikezang macrumors 6502a

    May 22, 2010
    Tokyo, Japan
    I save objects to managedObject in AppDelegate, I got error as below when read in my View controller, where did I make wrong

    2012-04-26 20:29:47.464 MyApp[1417:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Type''

    Now the error is changed to
    2012-04-26 22:33:30.304 MyApp[2329:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath not found in entity <NSSQLEntity Type id=6>'

    My Entity is like below with a relationship, but I din't set value to it when I save it because I don't have relationship object in Core Data, how can I save such data?

    @interface Type : NSManagedObject
    @property (nonatomic, retain) NSString * type;
    @property (nonatomic, retain) NSSet *types;
    @interface Type (CoreDataGeneratedAccessors)
    - (void)addTypesObject:(Type *)value;
    - (void)removeTypesObject:(Type *)value;
    - (void)addTypes:(NSSet *)values;
    - (void)removeTypes:(NSSet *)values;
    - (void)createType {
        Type *type = (Type *)[NSEntityDescription insertNewObjectForEntityForName:@"Type" inManagedObjectContext:self.managedObjectContext];
        type.type = @"Type1";
        type = (IssueType *)[NSEntityDescription insertNewObjectForEntityForName:@"Type" inManagedObjectContext:self.managedObjectContext];
        type.type = @"Type2";
        // Commit to core data
        NSError *error;
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"Failed to add default user with error: %@", [error domain]);
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        UINavigationController *navController = (UINavigationController *)[tabController.viewControllers objectAtIndex:0];
        AppViewController *appController = (AppViewController *)navController.topViewController;
        appController.managedObjectContext = self.managedObjectContext;
        // Get a reference to the stardard user defaults
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        // Check if the app has run before by checking a key in user defaults
        if ([prefs boolForKey:@"hasRunBefore"] != YES) {
            // Set flag so we know not to run this next time
            [prefs setBool:YES forKey:@"hasRunBefore"];
            [prefs synchronize];
            [self createType];
        return YES;
    //  When the view reappears, read new data for table
    - (void)viewWillAppear:(BOOL)animated
        [super viewWillAppear:animated];
        //  Error here
        self.typeList = [CoreDataHelper getObjectsForEntity:@"Type" withSortKey:@"Type" andSortAscending:YES andContext:self.managedObjectContext];
        //  Force table refresh
        [self.typeTableView reloadData];
  2. MattInOz macrumors 68030


    Jan 19, 2006
    First thing to check when this error gets thrown what is the value of the ManagedObjectContext your asking for the Entity?

    You can do this with breakpoint in the right place.

    Really you want to check that it's not Nil.
    There was a change around iOS5 that a view controller might start asking the context for answers before the AppDelegate has passed the context to this ViewController.

    If that is the case then you need to pull the context from the AppDelegate.
  3. mikezang thread starter macrumors 6502a

    May 22, 2010
    Tokyo, Japan
    Thanks fou your advice, I found where has bug.

Share This Page