0
votes

Hi I am a begginer with IOS and have created my first core data app. I have created my modal and included it in the project. It does contain the entity 'Client'. When I use the following test code to create the database and attempt to add a Entity I get the following error

..'+entityForName: could not locate an NSManagedObjectModel for entity name 'ItemMake''

when calling [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:contxt];

I have checked everything I can think off, the code is shown below. This is some further information...

a) mdl and contxt are not nil.

b) database file is created in the simulator (16kb)

c) accessing the database from another tool shows only two tables which appear to be SQLlite system tables

d) The package does contain a 'EasyOrder.momd' directory and a 'EasyOrder.mom' file. The app is called 'EasyOrder'.

e) The project contains a 'EasyOrder.xcdatamodeld' file which has a Client entity (among others) defined.

f) I have tried other enities in my database with the same results

g) My was a standard app that I added core data support too. I am not using default code in the app delegate, but my examples do not either.

Code as follows

NSManagedObjectContext *contxt;
NSManagedObjectModel *mdl;
NSPersistentStoreCoordinator *ps;

mdl = [NSManagedObjectModel mergedModelFromBundles:nil];
ps = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mdl];


NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);                

NSString *docDir = [documentDirectories objectAtIndex:0];

NSString *dbLoc = [docDir stringByAppendingPathComponent:@"test.sqllite"];

NSURL *databaseUrl = [NSURL fileURLWithPath:dbLoc];

NSError *err;

if ([ps addPersistentStoreWithType:NSSQLiteStoreType
                     configuration:nil 
                               URL:databaseUrl 
                           options:nil 
                             error:&err] == false) {
    NSLog(@"%@", err);
}

[contxt setUndoManager:nil];



//Add record

//CRASHES HERE !

Client *c = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:contxt];


[c setEntityRef:1234];

....

tia Rob

1
What do you have in your model or in your code that would make the app want to create an Entity with the name "ItemMake"?sosborn
Sorry the error was from oenn of my other tests. I changed from @"ItemMake" to @"Client" in the line after //CRASHES HERE in my sample code.user1516268

1 Answers

1
votes

I have just solved the issue. I believe there was some sort of versioning problem where the data model being used was old and contained no tables.

I created a test app with a dummy model (just one table) and it worked fine. I then removed that model and used the one from my actual application. It continued to work fine but did not created the tables as per the data model. So I removed the run time from the simulator and it then properly created all tables.

I followed the same process in my actual app, removed from simulator, removed model, added model with a different name and re built. App now works !