Reputation: 1647
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...
Upvotes: 0
Views: 1432
Reputation: 956
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.
Upvotes: 1
Reputation: 3429
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.
Upvotes: 0