【问题标题】:could not locate an NSManagedObjectModel for entity name "X"找不到实体名称“X”的 NSManagedObjectModel
【发布时间】:2012-03-02 11:25:09
【问题描述】:

我有这个崩溃错误:* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“JourneeDeTravail”的 NSManagedObjectModel”

我的 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

我的 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

还有一个 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

还有 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];
}

但是当我按下 SaveTest 按钮时,它会因错误日志而崩溃,说我的所有日​​志行都是(空)。

当然,我有一个名为 JourneeDeTravail 的实体...

有什么想法吗?看起来我没有 managedObjectContext 但我不知道该怎么做才能解决这个问题。感谢您的帮助!

【问题讨论】:

    标签: core-data ios5 nsmanagedobjectcontext


    【解决方案1】:

    调试器说明一切。它找不到实体。在您的情况下,实体是存储。没有核心数据,没有内存。

    在 AppDelegate.m 中 applicationDidFinishLaunching 尝试实例化一个存储类。这就是 ManageObjectModel 搜索的实体。

    点赞:myMainThreadStorage = [[StorageClass alloc] init];

    或者我完全误解了过去 6 小时研究我的问题并用后来的[super viewDidLoad]解决了它

    【讨论】:

      【解决方案2】:

      仅针对那些有同样问题的人:我在完成本教程时找到了解决方案:http://www.techotopia.com/index.php/An_iOS_5_iPhone_Core_Data_Tutorial

      【讨论】:

        猜你喜欢
        • 2011-03-21
        • 1970-01-01
        • 2012-07-20
        • 2011-10-29
        • 2011-05-14
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        相关资源
        最近更新 更多