在CoreData CLI中导入.xcdatamodel

时间:2010-08-26 18:20:43

标签: cocoa macos core-data nsexception

我对CoreData命令行工具有点问题。我只是在XCode(Core Data CLI)中创建了一个新项目,在.xcdatamodel文件中创建了一个数据模型并自动生成了一个Header文件。最后我修改了代码,它应该能够加载实体。

程序编译没有错误,但如果我运行程序,我会收到以下错误

2010-08-26 20:16:52.031 Core Data Test[866:903] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Kunde''
*** Call stack at first throw:
(
 0   CoreFoundation                      0x00007fff884bbcc4 __exceptionPreprocess + 180
 1   libobjc.A.dylib                     0x00007fff85a630f3 objc_exception_throw + 45
 2   CoreData                            0x00007fff821467a9 +[NSEntityDescription entityForName:inManagedObjectContext:] + 153
 3   CoreData                            0x00007fff821798a1 +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 33
 4   Core Data Test                      0x0000000100001abf managedObjectModel + 95
 5   Core Data Test                      0x0000000100001b7f managedObjectContext + 156
 6   Core Data Test                      0x00000001000019b7 main + 47
 7   Core Data Test                      0x0000000100001980 start + 52
 8   ???                                 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Abort trap

这是我的代码:svn://homes.dnsalias.com/CoreDataOwnTesting或简单复制

svn co svn://homes.dnsalias.com:3690/CoreDataOwnTesting

有没有人知道我做错了什么?谢谢你的帮助! 丹尼斯

JLN添加的代码

NSManagedObjectModel *managedObjectModel();
NSManagedObjectContext *managedObjectContext();
NSManagedObjectContext *context;


int main (int argc, const char * argv[]) {

    objc_startCollectorThread();

    // Create the managed object context
    context = managedObjectContext();

    NSManagedObjectModel *kunde = managedObjectModel();

    // Save the managed object context
    NSError *error = nil;    
    if (![context save:&error]) {
        NSLog(@"Error while saving\n%@",
              ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
        exit(1);
    }
    return 0;
}



NSManagedObjectModel *managedObjectModel() {

    static NSManagedObjectModel *model = nil;

    if (model != nil) {
        return model;
    }

    model = [NSEntityDescription insertNewObjectForEntityForName:@"Kunde" inManagedObjectContext:context];

    /*NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
    path = [path stringByDeletingPathExtension];
    NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"mom"]];
    model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    */

    return model;
}



NSManagedObjectContext *managedObjectContext() {

    static NSManagedObjectContext *context = nil;
    if (context != nil) {
        return context;
    }

    context = [[NSManagedObjectContext alloc] init];

    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel()];
    [context setPersistentStoreCoordinator: coordinator];

    NSString *STORE_TYPE = NSSQLiteStoreType;

    NSString *path = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
    path = [path stringByDeletingPathExtension];
    NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];

    NSError *error;
    NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];

    if (newStore == nil) {
        NSLog(@"Store Configuration Failure\n%@",
              ([error localizedDescription] != nil) ?
              [error localizedDescription] : @"Unknown Error");
    }

    return context;
}

1 个答案:

答案 0 :(得分:0)

看起来您在设置核心数据堆栈时遇到了一些基本问题。也就是说,第一个问题(我停在那里)是你试图从NSEntityDescription创建一个NSManagedObjectModel实例。吊杆。

您是否阅读并关注Core Data Utility Tutorial?您可以更轻松地按照教程并报告您在那里找到的问题,而不是在这里鹦鹉学舌。