0

I have a singleton data controller to hold an array of objects. See for example bananas question for my solution: singelton dataController banansArray

Now I want to save the array of bananas to persistant state. This core data tutorial: core data - store images have given me a good general understanding of Core Data and I was able to include it in my application before changing my data Controller to singleton.

Now what is best?

Do I need to move the generated Core Data stack within the application delegate to the singletonDataController that manage the bananas array? Or do I have to set the context of the singleton in the application delegate as you do in the generated Master-View controller with Core Data template?

In that case how do I set the context in the appDelegate? This does not work (it workes for the masterView in template) in the AppDelegate application didFinishLaunchingWithOptions:

DataControllerSingleton *dataController;
dataController.managedObjectContext = self.managedObjectContext;

In beerDataModel example provided the ManagedObjectCode is:

if (_mainContext == nil) {
    _mainContext = [[NSManagedObjectContext alloc] init];
    _mainContext.persistentStoreCoordinator = [self persistentStoreCoordinator];
}
Community
  • 1
  • 1
djcj
  • 149
  • 2
  • 15
  • I have seen the answer in this post: http://stackoverflow.com/questions/6622699/singleton-managedobjectcontext and please feel free to further comment if its advisable to use singleton and coreData in this way – djcj May 16 '13 at 17:44
  • 1
    Personal choice. I'd move it to the singleton so the ownership and management is all in one place. The singleton is also easier to get a reference to in your classes that need access to the Model (compared to the app delegate). – Wain May 16 '13 at 17:58

1 Answers1

2

Based on your question, I think it's a personal choice. For example, within my project I prefer to maintain a singleton class for managing the Core Data stack and use it throughout the application. I prefer to leave the app delegate clean.

Anyway, now if you use Core Data the old singleton, the one that manages the arrays of objects, is not useful anymore. With Core Data you have a graph of objects that can be grabbed from a persistent store (for example). Obviously you need to design your model correctly (entities, relationships, etc.). In your case, for example, a Banana entity with the correct attributes is the right choice.

To see in action a singleton class take a look at BeerDataModel.h/.m by @BenSheirman. This is a very good approach to follow. It can be used like the following.

NSManagedObjectContext *mainContext = [[BeersDataModel sharedDataModel] mainContext];

P.S. Change BeersDataModel in BananasDataModel or what name you prefer.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Thank you for the info! I am a bit confused on where to place your code suggestion? BeerDataModel seem to use: - (NSManagedObjectContext *)mainContext { if (_mainContext == nil) { _mainContext = [[NSManagedObjectContext alloc] init]; _mainContext.persistentStoreCoordinator = [self persistentStoreCoordinator]; } – djcj May 22 '13 at 21:50
  • @djcj You're welcome. But I cannot understand what you mean. Could you edit your question and put the request there? In the meantime, if you want to up vote... ;) – Lorenzo B May 23 '13 at 07:53
  • I was just wondering where to put your code suggestion? I cant find that line of code in the beersDataModel. But I think I have implemented I do however have a new issue. Would very much appreciate if you took at look at it: http://stackoverflow.com/questions/17429203/nsmanaged-context-threads?noredirect=1#comment25347242_17429203 – djcj Jul 03 '13 at 12:43
  • @djcj That line of code can be used from other sources. So, whenever you need an instance of your main context (in an external class) grab it like the snippet above. I will look at your question. Cheers. – Lorenzo B Jul 03 '13 at 15:03