Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:35:03+00:00 2026-06-01T20:35:03+00:00

I am playing around, learning CoreData and when I got a test application with

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:35:05+00:00Added an answer on June 1, 2026 at 8:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I am currently learning about basic networking in java. I have been playing around
I playing around with the Twitter API for my BlackBerry application. Is there any
Been playing around with this for a couple of hours and can't seem to
I'm still learning python and after playing around with pygame I noticed I'm re-importing
I'm learning ASP.net and I've been playing around with themes and master pages. I
I'm learning Python and I have been playing around with packages. I wanted to
I've been playing around with boost threads today as a learning exercise, and I've
I'm playing around with a fork of acts_as_taggable_on_steroids as a learning exercise. The version
I am learning C and I am playing around with pointers and arrays. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.