Reputation: 31995
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.
Upvotes: 0
Views: 2077
Reputation: 6990
As discussed in the comments above, it looks like you probably needed to check how your ManagedObjectModel
was being initialized.
I'd personally recommend avoiding placing Core Data code directly in your app delegate - I don't think Apple's templates do this very well. Check out this blog post as a great example of the minimal amount of code that's required to set up a Core Data stack, as well as a brief explanation of what each part does.
Regarding your extra comment question - the managedObjectContext
method you have is called whenever your managedObjectContext
property is accessed. So, when you do:
listcontroller.managedObjectContext = self.managedObjectContext;
This calls the managedObjectContext
method on self
, which (if I remember correctly) will initialize your context.
Upvotes: 2