I'm now building up Core Data based iOS application and when I tried to insert new managed object by executing [NSEntityDescription insertNewObjectForEintityForName:@"myModel" inManagedObjectContext:_managedObjectContext];
within AppDelegate.m
, I got the error described on the title.
Here's my AppDelegate.h
file:
#import
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
// maybe required?
//@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@end
And here's my AppDelegate.m
file (only displaying the relevant part):
#import "AppDelegate.h"
#import "MyModel.h"
#import "listViewController.h"
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//managed object context settings
UITabBarController *tabbarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [[tabbarController viewControllers] objectAtIndex:0];
listViewController *listcontroller = [[navigationController viewControllers] objectAtIndex:0];
listcontroller.managedObjectContext = self.managedObjectContext;
NSLog(@"%@", _persistentStoreCoordinator.managedObjectModel);
MyModel *newMyModel = [NSEntityDescription insertNewObjectForEntityForName:@"MyModel" inManagedObjectContext:_managedObjectContext];
return YES;
}
in this application, I want to use tabbar controller as root view controller and when the app starts, I want to navigation controller as the tabbar controller's root view controller, and use tableview controller as the navigation controller's root view controller. And in the table view controller, I want to use Core Data functionality to display a lot of entities to users.
If I used breakpoint at the exact point of the NSLog()
output, it didn't return any errors. And when I moved one line forward to output the log message, the following output returns:
() isEditable 0, entities {
}, fetch request templates {
}
which means I don't have entities for some reasons.
So why are there no entities in this situation? From this answer here over SO, I don't misspelled my entity name. Also, my objectModelContext is not set to nil. So did I set the wrong managed object? Am I doing something wrong in the first three lines in didFinishLaunchingWithOptions
method?
Or is there something that is causing the issue here? Or what am I missing?
I use iOS 7 and Xcode 5 and I don't have any managed objects in my entity - after all, the error happened when I tried to instantiate those managed objects.
Thanks.
ManagedObjectModel
? – James FrostManagedObjectModel
created automatically by writing@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
in header and@synthesize managedObjectModel = _managedObjectModel;
in implementation file, right? – BlaszardManagedObjectModel
property. You need code to initialize / create the actual model. By default, Xcode places this into a method in your AppDelegate. – James FrostManagedObjectModel
. The method-(NSManagedObjectContext *)managedObjectContext
withinAppDelegate.m
is called when I start the app, right? Then, you're right, that editing its method enables my app to work now. I actually copied and pasted its code from another template, because as I said, I have to use tabbar controller as root (not having Core Data at default) and have not changed its model name to new one. – Blaszardapplication: didFinishLaunchingWithOptions:
method, themanagedObjectContext
method isn't called. So then where and when is it called in the app? – Blaszard