1

I have searched for this but the answer is too simple to satisfy me :(

I want to pass a NSManagedObjectContext from TabBarController to almost every view controllers in my app. In detail, my storyboard structure is kind of like this:

TabBarController >>> several tab items views ( this is easy to pass, just pass one time in according view controllers). But I have another relation: TabBarController(the one mentioned above) >>> NavigationController >>> TableViewControllerOne >>> TableViewControllerTwo,

now it's disaster. Because basically I have to transfer the NSManagedObjectContext instance from the NavigationController to TableViewControllerOne and to TableViewControllerTwo....that could involve a lot of prepareSegue:sender and it's not easy to manage.

So my question is: in iOS development, is there a way that I can create a "global" object that I can easily access in my entire app? From apple's official template for master-detail view using core data, it instantiates the NSManagedObjectContext in app delegate, passing it to master view controller. What if I have to use this object after several view pass? Wow, that's a lot of code to do.

Hope I made myself clear and someone could help :) Thanks a lot.

geo
  • 1,781
  • 1
  • 18
  • 30
JimZ
  • 1,182
  • 1
  • 13
  • 30
  • 2
    You can consider creating a singleton. Check out http://stackoverflow.com/questions/145154/what-should-my-objective-c-singleton-look-like – savner May 16 '13 at 13:17
  • There's a great post about getting a singleton out of UIManagedDocument http://www.adevelopingstory.com/blog/2012/03/core-data-with-a-single-shared-uimanageddocument.html – Alex May 16 '13 at 15:48

3 Answers3

2

I would seriously recommend using the MagicalRecord library for your CoreData stack requirements.

You can then do things like:

[NSManagedObjectContext MR_defaultContext];

or

[NSManagedObjectContext MR_contextForCurrentThread];

And setting up your CoreData in the first place can be as simple as:

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"myDatabase.sqlite"];

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
1

I use the following shared store idea, which creates a singleton. This has been fairly flexible for me in storing different things globally (The code is adapted from iOS Programming The Big Nerd Ranch Guide).

In the code below I have a PartStore class which will always return the same PartStore instance.

To get access to this instance just include PartStore.h in the class which needs access to the shared store and retrieve the instance using the class method:

[PartStore sharedStore];

You then have access to any of the properties contained within the instance of that class.

// PartStore.h
#import "PartData.h"

@interface PartStore : NSObject

@property (nonatomic, strong) PartData *scannedData;

+ (PartStore *)sharedStore;

@end

// PartStore.m
@implementation PartStore

+ (PartStore *)sharedStore
{
    static PartStore *sharedStore = nil;
    if (!sharedStore) {
        sharedStore = [[super allocWithZone:nil] init];
    }

    return sharedStore;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedStore];
}


@end
Pliskin
  • 1,259
  • 10
  • 11
1

Personally I like to use a singleton object of a ModelController class where I can put Core Data stack-related objects (Context, Model and Store Coordinator) and helper methods all in one place, that I can then access from anywhere in my view controllers. Something like this:

@implementation ModelController

- (NSManagedObjectModel *)managedObjectModel
{
    // ...
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    // ...
    return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)contextForCurrentThread
{
    NSManagedObjectContext *context = [[NSThread currentThread] threadDictionary][@"threadManagedObjectContext"];
    if (context == nil)
    {
        context = [[NSManagedObjectContext alloc] init];
        [context setPersistentStoreCoordinator:self.persistentStoreCoordinator];

        [[NSThread currentThread] threadDictionary][@"threadManagedObjectContext"] = context;
    }
    return context;
}

- (void)resetCoreDataStack
{
    [[[NSThread currentThread] threadDictionary] removeObjectForKey:@"threadManagedObjectContext"];
    self.persistentStoreCoordinator = nil;
    self.managedObjectModel = nil;
}

+ (ModelController *)sharedModelController
{        
    static ModelController *modelController;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        modelController = [[ModelController alloc] init];
    });
    return modelController;
}
@end
neural5torm
  • 773
  • 1
  • 9
  • 21