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 539587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:09:05+00:00 2026-05-13T10:09:05+00:00

I see how to solve the problem but it bothers me that I don’t

  • 0

I see how to solve the problem but it bothers me that I don’t understand why this doesn’t work. I have a UIViewController subclass that uses Core Data, so it needs the NSManagedObjectContext. The controller is loaded from a nib file where it’s placed under a navigation controller which is inside a tab controller.

I tried doing this in initWithCoder and viewDidLoad and for some reason it doesn’t work:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = [[appDelegate managedObjectContext] retain];

For some reason managedObjectContext returns nil and I get this when I try to create a managed object later:

*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘+entityForName: could not locate an entity named ‘LogRecord’ in this model.’

Which is what you get when your context is nil or the model can’t be loaded (or really lacks the entity).

If I do the exact same thing at the top of my saveLogEntry method (which creates managed objects and saves the context) then it works just fine.

If I do what the Recipes sample application does:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    loggingViewController.managedObjectContext = self.managedObjectContext;

    // Standard stuff
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
}

(loggingViewController is an IBOutlet in the app delegate).

Does anyone know what specifically might be going on here? It seems like it fails if done “too early” but especially with viewDidLoad I’d expect it to work since I think that occurs after addSubview is called.

  • 1 1 Answer
  • 3 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-05-13T10:09:05+00:00Added an answer on May 13, 2026 at 10:09 am

    Do exactly what the recipes app does.

    If you try it in initWithCoder, you don’t know if the app delegate has finished initialization (which it hasn’t)

    If you try it viewDidLoad, you have a similar problem.

    That is why you should NOT be accessing the app delegate like so:

    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [[appDelegate managedObjectContext] retain];
    

    This is bad form. It introduces coupling into your design. Use dependency injection, just like the example. It makes your app more flexible.

    Because from the app delegate you know exactly what initialization has been performed and can pass in the context at the appropriate time.


    Update:

    The issue is that your View Controller instance is likely being instantiated in the Mainwindow.xib. Mainwindow.xib (and any other nibs it references) is “defrosted” before the app delegate receives UIApplicationDidFinishLaunchingNotification notification.

    The order in which objects are defrosted from nibs is not guaranteed. When initWithCoder: is called on your View Controller you have no idea what other objects have been defrosted from the nib. You also can’t be sure whether the app delegate has received the UIApplicationDidFinishLaunchingNotification notification.

    It is similar for viewDidLoad. In viewDidLoad, you can be sure that all other objects in the nib have been properly defrosted and initialized, but since the configuration of the app delegate happens outside of the nib file, you can’t be sure whether it is safe to call the app delegate.

    It is better just to have the app delegate pass in the context when it is “good and ready”, preferably in the applicationDidFinishLaunching: method.

    Hope that is a little clearer, you should take a look at the iphone programming guide:
    http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/index.html

    To glean a better explanation of the iPhone application life cycle.

    Hope that helps.


    One More Update:

    In depth discussion of the iphone launch sequence:
    http://www.bit-101.com/blog/?p=2159

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

Sidebar

Related Questions

See the image below. How I can solve this problem? I think that is
I see this link But it's not solve my problem. I want to ask
I've been trying to solve this problem but I really can't see what's wrong
I can't see this in the docs, but I presume it's a solved problem.
I've been trying to solve this problem for quite sometime but I am having
i spend my hours to solve this problem but cannot reach anything.I applied what
For few days now I'm trying to solve this problem. I have table group_user,
i would like to have a query that will solve my problem in native
I have problem that seems to be rather trivial but I was not able
I am trying to solve this problem myself but I can't. So I want

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.