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

  • Home
  • SEARCH
  • 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 7068397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:19:08+00:00 2026-05-28T05:19:08+00:00

I used the default template provided by Apple with Core Data (managedObjectContext is in

  • 0

I used the default template provided by Apple with Core Data (managedObjectContext is in AppDelegate). At first I was including appdelegate.h in every classes I needed to use managedObjectContext, but I saw this was not the correct way to do it. Apple says it’s better to only pass the context to other classes that need it and so on, so I ended up doing it that way. Thing is, it looks a bit “hackerish” the way I did it, and I’m wondering if there’s a better option or my solution is correct.

My app is currently setup like that (here’s a SS of my storyboard):
enter image description here

So my root window is a UITabBarController, and each tab is a UINavigationController that points to multiple UITableViewController/UIViewController.

Here is what I have in my Appdelegate to pass the managedObjectContext instance to 2 tabs:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController *rootViewController;
    UINavigationController *navigationController;
    ItemsTableViewController *itemsTableViewController;

    // Get the root window (UITabBarController)
    rootViewController = (UITabBarController *)self.window.rootViewController;


    // Get the second item of the UITabBarController
    navigationController = [[rootViewController viewControllers] objectAtIndex:1];

    // Get the first item of the UINavigationController (ItemsTableViewController)
    itemsTableViewController = [[navigationController viewControllers] objectAtIndex:0];
    itemsTableViewController.managedObjectContext = self.managedObjectContext;


    // Get the third item of the UITabBarController (again ItemsTableViewController)
    navigationController = [[rootViewController viewControllers] objectAtIndex:2];

    // Get the first item of the UINavigationController (ItemsTableViewController)
    itemsTableViewController = [[navigationController viewControllers] objectAtIndex:0];    
    itemsTableViewController.managedObjectContext = self.managedObjectContext;

    return YES;
}

Everything works well, but having to call multiple times objectAtIndex to get to the right ViewController looks meh…

Anyone as a better solution?

Thanks!

  • 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-05-28T05:19:08+00:00Added an answer on May 28, 2026 at 5:19 am

    You should look at using the prepareForSegue: method to pass your managedObjectContext to the other controllers.

    Alternatively, you can subclass the tab bar controller and add the managed object context as a property, which you can then access from anywhere within your app provided the tab bar controller is also there.

    Finally, if you are only ever going to use one context (i.e. no multi threads) you can always setup a CoreDataHelper class with a class method that returns your default context whenever you ask for it. To avoid importing the helper in every single class just add the helper to your precompiled header file (.pch) and let it also import the <CoreData/CoreData.h> framework.

    If you want to see an example of how this is done, checkout MagicalRecord on github https://github.com/magicalpanda/MagicalRecord

    [EDIT]
    Here’s an example of how you would pass the context using the prepareForSegue method. Remember that this method is called when a segue is about to initiate and it gives you the opportunity to setup the view controller that is about to be pushed. This is where you could pass delegate references and assign values to other variables in your destination view controller:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        NSString *segueIdentifier = [segue identifier];
        if ([segueIdentifier isEqualToString:@"YourSegueIdentifier"]) // This can be defined via Interface Builder
        {
            MyCustomViewController *vc = [segue destinationViewController];
            vc.managedObjectContext = self.managedObjectContext;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As we use default keyword as a access specifier, and it can be used
I am creating an iPad application using a UISplitView. I used the default template
The default split view template for a Metro application written in C# makes use
Does anyone know where I can find the default Silverlight Project Template used in
Is there a way to change the default pages used to edit/create/view a Sharepoint
I recently had to work around the different default sizes used for enumerations in
is there any way to reserve memory space to be used later by default
where used on http://localhost:8692/Contacts/Default.aspx Response.Redirect( http://www.google.com ) redirects to http://localhost:8692/Contacts/http%3a%2f%2fwww.google.com The problem just appeared
What is default type of JIT compiler is used in .Net (visual studio) out
Do you know the default isolation level of the transactions used in Django? Is

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.