Fabien Lebas
Fabien Lebas

Reputation: 503

could not locate an NSManagedObjectModel for entity name "X"

I have this crash error : * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'JourneeDeTravail''

My AppDelegate.h :

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;     
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator          *persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
@end

My AppDelegate.m :

#import "AppDelegate.h"
#import "TableViewController.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize managedObjectContext =_managedObjectContext;
@synthesize managedObjectModel =_managedObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *viewController = [[ViewController alloc] init];
viewController.managedObjectContext = [self managedObjectContext];
NSLog(@"AppDelegate VC: %@",  managedObjectContext);
return YES;
}

- (void)dealloc {
[super dealloc];
}

@end

And a ViewController.h :

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController{
NSManagedObjectContext *managedObjectContext;
}
@property (retain, nonatomic) IBOutlet UILabel *displayStart;
@property (retain, nonatomic) IBOutlet UILabel *displayEnd;
@property (retain, nonatomic) IBOutlet UITextField *displayResult;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

- (IBAction)SaveTest:(id)sender;

@end

And ViewController.m :

#import "ViewController.h"

@implementation ViewController
@synthesize managedObjectContext;

- (void)viewDidLoad {    
if (managedObjectContext == nil) { 
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
    NSLog(@"After managedObjectContext VC: %@",  managedObjectContext);
}
}

- (IBAction)SaveTest:(id)sender {
NSLog(@"Dans SaveTest : %@",  managedObjectContext);
NSLog(@"Context: %@",managedObjectContext);
NSLog(@"PS Coord : %@",managedObjectContext.persistentStoreCoordinator);
NSLog(@"MOM : %@", managedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",  [[managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"JourneeDeTravail"]);
JourneeDeTravail *journee = (JourneeDeTravail *)[NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:managedObjectContext];
}

But when I press SaveTest button it crashes with error log saying all my log lines are (null).

Of course I have an Entity called JourneeDeTravail...

Any idea ? Looks like I don't have a managedObjectContext but I don't know what to do to fix that. Thanks for your help !

Upvotes: 0

Views: 3411

Answers (2)

MOVT
MOVT

Reputation: 21

The debugger says all. It can't find the entity. The entity is in your case the storage. No Core Data, no Memory.

In AppDelegate.m in time of applicationDidFinishLaunching try to instantiate a storage class. Thats the entitiy the ManageObjectModel search for.

like: myMainThreadStorage = [[StorageClass alloc] init];

Or I have completely misunderstood the last 6 hours of studying my problem and solved it with a later [super viewDidLoad]

Upvotes: 1

Fabien Lebas
Fabien Lebas

Reputation: 503

Just for those of you who have the same problem : I found the solution when I did this tutorial : http://www.techotopia.com/index.php/An_iOS_5_iPhone_Core_Data_Tutorial

Upvotes: 1

Related Questions