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?
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];
}
Last edited: