Where to put the "Core Data Stack" in the Cocoa / Cocoa Touch app

In the iPhone core data template, Apple places the main data stack in the application delegate.

However, my initial inclination is to move this code to my class, whose responsibility is to handle the management of the underlying data stack.

Do you usually encapsulate this functionality in your class or will you leave it in the App Delegate?

+67
iphone cocoa-touch cocoa core-data
Aug 12 '09 at 17:19
source share
6 answers

Summary: There is no need to create a singleton to control the Core Data stack; indeed, it is likely to be counterproductive.

The Core Data stack is created by the application delegate. It is important to note, however, as all examples show, the stack (mainly the context of the managed object) is not retrieved directly from the stack (*). Instead, the context is passed to the first view controller, and from them in the context, or the managed object is passed from one view controller to the next (as described in Accessing the master data stack ). This follows the basic template for the iPhone of all applications: you transfer data or a model controller from one view controller to the next.

A typical singleton role, as described here, is a model controller. With Core Data, the context of managed objects is already a model controller. It also gives you the ability to access other parts of the stack, if necessary. Moreover, in some situations (as described in the documentation), you can use a different context to perform a discrete set of actions. The corresponding unit of currency for the view controller, therefore, is usually the context of a managed entity, otherwise a managed entity. Using and passing a singleton object that controls the stack (and from which you are extracting the context) typically at best introduces an unnecessary level of indirection and at worst introduces excessive application rigidity.

(*) No example retrieves context using:

[[UIApplication delegate] managedObjectContext]; 
+39
Jan 05 '10 at 8:16
source share

I have a singleton class that allows you to perform basic data management, and I do not leave it on the application’s folder. I prefer not to clutter the application delegate class with methods that may be needed for convenience, such as selecting specific objects, etc.

+28
Aug 12 '09 at 17:26
source share

I leave the main data logic in the application for the following reasons:

1) I do not see a real advantage in promoting this code in other classes: the concept of delegation is fully implemented by the core data logic processed by the application delegate, since the core data model is actually a fundamental part of your application;

2) In the entire code sample that I saw, including Apple samples, the master data material is processed by the App delegate;

3) Even in Core Data books, application delegation with key code related to data is usually used;

4) Personally, I don’t think that readability or anything else is actually improved due to the availability of special classes for the main data, but this is a matter of personal taste, and I will not argue about which approach is the best. For me, simplicity is important while maintaining functionality.

+11
Aug 12 '09 at 18:07
source share

The question I asked myself, in your case, is “who has the“ basic data stack ”? The data itself is really the application area, isn't it? (CF Core Data on Mac, where you can have an application that can work with multiple documents at once, so the Core Data stack belongs to each document.)

In any Cocoa / Cocoa Touch application, an application delegate is usually the preferred means of customizing application behavior, which is why this is the natural place for the Core Data stack.

Now the problem that I suspect is that he doesn’t feel so constant to write things like:

 NSManagedObjectContext *context = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

What I usually do in these cases are write functions (not methods) like this:

 NSManagedObjectContext *UIAppManagedObjectContext() { return [*(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } 

I am writing a similar function for NSPersistentStoreCoordinator and NSManagedObjectModel . I put all these files in the .h / .m delegate files, as these are also application level objects.

+10
Aug 12 '09 at 18:30
source share

I just listed this in a new answer. (I discarded my previous FJSCoreDataStack class in favor of this)

My new way to handle this was to use a category in NSManagedObjectContext. Ive added the following class methods:

 + (NSManagedObjectContext *)defaultManagedObjectContext; + (NSManagedObjectContext *)scratchpadManagedObjectContext; + (NSManagedObjectModel *)managedObjectModel; + (NSPersistentStoreCoordinator *)persistentStoreCoordinator; + (NSString *)applicationDocumentsDirectory; 

This keeps everything from my application delegate and gives one-way access if I want to use it. However, I still use dependency injection from the App Delegate (as mmalc said, it introduces inflexibility into my code). I just moved all the "Core Data Stack" code to the NSManagedObjectCOntext category.

I like to pass the link around, especially since I have a good "context" method. This allows me to control my View controllers in a flexible way, because I did not pass them "defaultManagedObjectContext".

Also relevant to the conversation in the iPhone world (and may be relevant to your architecture): NSFetchedResultsController and building NSFetchRequests

+6
Dec 17 '09 at 18:40
source share

I support the fact that the application delegate knows where the model starts, and having a model where the context of the managed object is located. The "basic information" of the model seems to me like a model for implementing the model, the controller classes (for example, the application delegate) should just ask "give me this information about the model", and the model should know how to answer this question. Therefore, having a Core Data object accessible through a controller object seems like a simple abstraction.

+4
Aug 12 '09 at 20:21
source share



All Articles