0

i've a little Problem with a CoreData Command Line Tool. I simply created a new Project in XCode (Core Data CLI), created a Data Model in the .xcdatamodel File and auto generated a Header file. Finaly i modified the Code, wich should able to load the Entity.

The Program Compiles without errors, but if i run the Program i get the following error

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

Here's my code: svn://homes.dnsalias.com/CoreDataOwnTesting or simple to copy

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

Does anyone have an Idea what i am doing wrong? Thank's for your help! Dennis

Code added by 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;
}
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
Dennis Stritzke
  • 5,198
  • 1
  • 19
  • 28

1 Answers1

0

It looks like you've got some basic problems in the way you're setting up the Core Data Stack. Namely, the first problem (and I stopped there) is that you're trying to create an NSManagedObjectModel instance from an NSEntityDescription. Boom.

Have you read and followed the Core Data Utility Tutorial? It's easier for you to follow the tutorial and report on the problems you find there than to parrot its instructions here.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Yes i have some Basic Problems with Core Data, but i Read (and tried it). Ok, i see stupid error. But also i do Not able to Load an xcdatamodel. New question: How do i add a xcdatamodel to a NSManagedObjectModel? Sorry for my inexplicit first question... – Dennis Stritzke Aug 27 '10 at 06:15
  • So ... what's your new code and what errors are you seeing? It's impossible to help you if we don't know what's wrong. – Joshua Nozzi Aug 28 '10 at 14:12