I have some model classes which I wanted to store with the CoreData framework. I have created the xcdatamodel file and added these already existing classes to the CoreData model file. I have also copied the code responsible for managing the context, etc. from the core data project template from the XCode.
Now I have the following code (it was copied, only the file name was changed to HistoryDataModel):
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"HistoryDataModel" ofType:@"momd"];
NSLog(@"Model path:", modelPath);
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel;
}
Now when I run this code and try to access data in the CoreData I'm getting the following error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
and it happens when
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"HistoryDataModel" ofType:@"momd"];
is being executed (the model Path is an empty string).
Any ideas what I might have done wrong?
Thanks!
Best Answer
You could try the following snippet, which will save you a lot of time :)
It returns a model created by merging all the models found in all the bundles (nil parameter). You can set the parameter to a specific array of bundles.