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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:22:13+00:00 2026-06-05T04:22:13+00:00

Just looking for a little feedback on if this is a bad idea for

  • 0

Just looking for a little feedback on if this is a bad idea for sharing the Managed Object Context.

In the MyApp-Prefix.pch file I have added following:

#import "AppDelegate.h"
#define MOC [(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext]

Then when I need to access the context I do the following (just an example):

[MOC deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

This seems to work great, but it also seems too easy, especially since I don’t see it mentioned anywhere. Is this a bad design pattern?

Thanks for any feedback

  • 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-05T04:22:14+00:00Added an answer on June 5, 2026 at 4:22 am

    Instead of a compiler macro (which can cause surprises sometimes) I usually define a class method on my app delegate, like this:

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

    Then, when I need a reference to some global state, I can call it like this:

    [[[AppDelegate sharedDelegate] managedObjectContext] deleteObject:foo];
    

    If your app is complex enough, it might be worth the trouble of passing around a reference to a managed object context, rather than using a global one. If you do, it becomes easier to refactor and use other techniques later, like child contexts to group changes.

    To be specific, that means each view controller has it’s own managedObjectContext instance variable. When you present a new view controller, you pass along a reference: maybe in a custom init method, maybe by setting a property. None of your classes (except maybe the root view controller) ever reference your global app delegate. This way, if you had a complicated editing view, you could give it a child context where it could save changes “temporarily” (to verify that all the objects are valid); if the user hits the cancel button, you just throw the whole context away.

    Also, if you’re always using the global context, you might have bugs that are hard to track down. For example, an edit view that doesn’t clean up after itself leaves an invalid object behind. Then the next time you go to save something unrelated, you get an error! Happened to me, was no fun to debug.

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

Sidebar

Related Questions

So, this one has me a little perplexed. I'm probably just looking at it
I'm just looking for a little guidance as to how i might implement a
I'm just looking for suggestions on the best way to do this... I need
I was just looking at this code and i don't understand what RETURN TRUE
I was just looking at the jQueryUI button plug-in and noticed this $(button, input:submit,
Sorry for the super basic question but I'm just looking for a little direction.
Im just looking for a little clarity to better my understanding of the Rails
just looking for a point in the correct direction.. So I've developed a little
Im fairly new to Java and im just looking for a little help Im
Basically just looking to see if a particular txt file exists on the server,

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.