0

I have made a a datamodel with an entity named "Status". I also created the appropriate classes so I can use the property's to get the attributes. But it doesn't seem like my app can find my datamodel :S

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:            (NSDictionary *)launchOptions
{
    // Database test.
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    Blabla *t =  [NSEntityDescription insertNewObjectForEntityForName:@"Status"     inManagedObjectContext:managedObjectContext];

I took the relevant code out. It stops on the last line giving me this error:

+entityForName: could not locate an entity named 'Status' in this model.'

I double checked the names. I really have an entity called Status. I dont get it :S I also checked if the data model is in my compile source. It is...

Thanks...

2 Answers 2

1

I know this is old, but I believe this is an issue with the Model Configuration. If anyone sees this post, check to see if the entity is in the "Default" configuration, or if you've added a different configuration, make sure the entity in question is added to the correct configuration.

Sign up to request clarification or add additional context in comments.

Comments

0

Since it looks like this is running just as the application launches, I am guessing you have not set up your managed object model and your persistent store coordinator before you created the NSManagedObjectContext...

So try something like:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myModel" withExtension:@"momd"];
NSManagedObjectModel* managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *dbURL = [documentsDirectory URLByAppendingPathComponent:@"myModel.sqlite"];

NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbURL options:nil error:&error]) {
    // handle error
}

NSManagedObject *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
Blabla *t =  [NSEntityDescription insertNewObjectForEntityForName:@"Status" inManagedObjectContext:managedObjectContext];

...

(release everything when you are done)

Of course, in a real application, you will probably want to keep these around as properties or instance variables in the application delegate and release them in the dealloc method.

2 Comments

Thanks for the answer, but I already had that code within my appDelegate. But I found the solution to the problem ^^ My model had to have the exact same name as my app... I called it data....momd Which isn't the same :) I could also change it in the model method ^^
I'm glad you solved you problem, although I am pretty sure that the data model does not need to have the same name as your app...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.