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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:25:29+00:00 2026-06-04T03:25:29+00:00

I am using CoreData and I set the NSManagedObjectContext in AppDelegate file. I need

  • 0

I am using CoreData and I set the NSManagedObjectContext in AppDelegate file.

I need to get that managedObjectContext in a ViewController which is many levels deep in the naviagation tree. Obviously, I do not want to pass it along all the init methods.

I have tried [[[UIApplication sharedApplication] delegate] managedObjectContext]; but I get this error “No known instance method for selector ‘managedObjectContext’“

Can someone guide me on how to get the managedObjectContext from AppDelegate to this ViewContoller?

  • 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-04T03:25:30+00:00Added an answer on June 4, 2026 at 3:25 am

    First you need to create a property in your AppDelegate.h like the following:

    @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; // or strong if you ARC instead of retain
    

    Using readonly prevent you to modify the context externally.

    In AppDelegate.m synthesize it like:

    @synthesize managedObjectContext;
    

    Always within the AppDelegate.m override the getter method like

    - (NSManagedObjectContext *)managedObjectContext
    {    
        if (managedObjectContext != nil) return managedObjectContext;
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
    
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator:coordinator];
        }
        return managedObjectContext;
    }
    

    Once done you have a managedObjectContext property which can be accessed anywhere with

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;
    

    A more cool approach could be to create a class method in your AppDelegate.h like the following:

    + (AppDelegate *)sharedAppDelegate;
    

    Then in AppDelegate.m do like the following:

    + (AppDelegate *)sharedAppDelegate
    {
        return (AppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    

    Now, anywhere, before having imported your AppDelegate header (#import "AppDelegate.h"), you can do:

    AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;
    

    Note

    Using such an approach causes your application to become rigid. To overcome this problem I suggest you to read about passing-around-a-nsmanagedobjectcontext-on-the-iphone by Marcus Zarra.

    Hope it helps.

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

Sidebar

Related Questions

Using CoreData, I have an entity Bookmark, that has an to-many relationship named 'tags'
I using CoreData to store my objects that I fetch from a server. I
I have a problem that whenever I'm inserting data using coredata, everything's going fine.
I am using CoreData in my app and i have a set of Card
I am using CoreData and have an Entity ContactList defined, which I created using
In my Core Data app I am using a FetchedResultsController. Usually to set titles
I am using CoreData and can retrieve my objects using [MyManagedObjectClass findAll] . Now
Still am using Coredata framework for iPhone applications. I want switch to use SQLite
I am using CoreData with iPhone SDK. I am making a notes app. I
I have a sample app using CoreData, and am now ready to convert my

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.