|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
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. |
|
|
|
0
|
|
|
#2 |
|
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] |
|
|
|
0
|
|
|
#3 |
|
Thanks fou your advice, I found where has bug.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:35 PM.






Linear Mode
