Reputation: 6814
I am playing around, learning CoreData and when I got a test application with a UITableView as the root, and it displays the data fine. This would tell me that the database it is pulling the data from is set up correct.
Now I am trying to to have a UIViewController as the initial view controller (via Story board) and it keeps giving me this error -
Cannot create an NSPersistentStoreCoordinator with a nil model
Now I have read a lot of forums and Tutorials and they all basically say that I need to make sure that my xcdatamodel file matches this
URLForResource:@"Model" withExtension:@"momd"]
and it does, again I can display the data if the UITableView is the root view.
Now if I change:
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
into this:
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
//NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]; // I have commented out this line because the NSURL is no longer being used.
__managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return __managedObjectModel;
}
The app runs but the TableView is empty, which makes sense to me, as I am not linking to the CoreData file. - The question is- how can I run the app with the setup I have got and show the data?
Cheers Jeff
@response to first answer by Jeff Wolski
Thanks for the answer, I think I am on the right path - I had to alter it slightly to my project to remove the errors. I know have
AppDelegate appDelegate = (AppDelegate) [[UIApplication sharedApplication] delegate];
So I have put this is the
- (NSManagedObjectModel *)managedObjectModel {
Area - is that correct? Also it now has come up with appDelegate variable is not used warning. May be a simple issue, but where do I use that variable now? Cheers Jeff
I have posted what I think is what you are after @Jody Hagins - Hope it helps:-)
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ViewToNav.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
Cheers Jeff
Upvotes: 0
Views: 291
Reputation: 6372
You need to set up all of your core data in your app delegate. The managed object model needs to be a property in your app delegate that your views can access. In your view controller, you can get a hook to your app delegate with
MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
This will allow you to perform a fetch from whatever view controller you like.
EDIT:
The reason we need to have the appDelegate
in the view controller is so that we can access the NSManagedObjectContext
that's in the app delegate. So you don't need to put the NSManagedObjectModel
in your view controller. The NSManagedObjectContext
(MOC) gives you access to the entire core data stack that already exists in your app delegate.
In your app delegate, your MOC should be set up as a property. Now you can do this in your view controller.
myMOC = appDelegate.MOC;
Now myMOC
gives you access to your core data. You will create a NSFetchedResultsController
as a property in your view controller. In your getter method, one of the steps will be to create a NSEntityDescription
. This is where you use your self.myMOC
, thus tying your NSFetchedResultsController
to your core data model.
Upvotes: 1