0
votes

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.

1
You could post your code that creates your ManagedObjectModel?James Frost
I don't follow what you mean. Is ManagedObjectModel created automatically by writing @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; in header and @synthesize managedObjectModel = _managedObjectModel; in implementation file, right?Blaszard
No - so those lines just declare the ManagedObjectModel property. You need code to initialize / create the actual model. By default, Xcode places this into a method in your AppDelegate.James Frost
Ah, OK. I didn't know how to initialize ManagedObjectModel. The method -(NSManagedObjectContext *)managedObjectContext within AppDelegate.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.Blaszard
And could you tell me one more thing then - In the application: didFinishLaunchingWithOptions:method, the managedObjectContext method isn't called. So then where and when is it called in the app?Blaszard

1 Answers

2
votes

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.