Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Where is best to put the Core Data code in a TabBar Application

Hey guys,

I'm working on a TabBar Application project and I'm using the Core Data framework.

I've followed Amit's courses, and so far I'm doing good :) .

In the AppDelegate class I have this code:

<pre> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UITabBarController *tabBarController;

@property (readonly, retain, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, retain, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, retain, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

  • (NSString *)iDeviceType;

  • (NSURL *)applicationDocumentsDirectory;

  • (void)saveContext; </pre>

Just like in the Treehouse iOS projects on the subject.

In my app, I have 4 sections: Pools, Players, (and Settings, and Help).

I've created the PoolsModel.xcdatamodeld generated the Pool class. In the Pools section, I have a "+" button and I can successfully add a Pool using Core Data.

My question is:

I've declared the 2 properties:

<pre> @property (retain, nonatomic) NSManagedObjectContext *managedObjectContext; @property (retain, nonatomic) NSManagedObjectModel *managedObjectModel; </pre>

in the "PoolsViewController.h" file, and obviously I'll have to do the same for the Players section.

Wouldn't be it more maintainable to subclass the "UITabBarController" class, and put these 2 properties, and also all the Core Data functionality for each Entity in that class, and than communicate with it?

Maybe someone who worked on more advanced Core Data projects knows which is the best approach ;) .

Thanks in advance

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@pavel if you are looking for efficiencies then you are better off using MagicalRecord. Just a heads up, for advanced topic questions like these you could easily find answers on StackOverflow.

Ok, Amit. Thanks a lot again