Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 26, 2012, 06:39 AM   #1
mikezang
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Error "'+entityForName: could not locate an NSManagedObjectModel"

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?

Code:
@interface Type : NSManagedObject

@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSSet *types;
@end

@interface Type (CoreDataGeneratedAccessors)

- (void)addTypesObject:(Type *)value;
- (void)removeTypesObject:(Type *)value;
- (void)addTypes:(NSSet *)values;
- (void)removeTypes:(NSSet *)values;

@end
Code:
- (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;
}
Code:
//  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];
}
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2

Last edited by mikezang; Apr 26, 2012 at 08:43 AM.
mikezang is offline   0 Reply With Quote
Old Apr 26, 2012, 06:46 PM   #2
MattInOz
macrumors 68020
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: Sydney
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.
__________________
There is no such thing as "Collective Wisdom"
[13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13", iPhone 4, iPad]
MattInOz is offline   0 Reply With Quote
Old Apr 26, 2012, 07:13 PM   #3
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Thanks fou your advice, I found where has bug.
mikezang is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC