0

I'm now developing an app with core data on iPhone. After reading many projects about core data, I find that core data code is embedded into viewController code in all of there projects. for example:

@interface MyAppMasterViewController : UITableViewController <NSFetchedResultsControllerDelegate>

@property (strong, nonatomic) MyAppDetailViewController *detailViewController;

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

I want to split all code about core data from viewController, and create a special storage class. what's more, provide some method to package core data api, for example:

@interface MyAppMasterViewController : UITableViewController
...
...
@interface MyAppStorageClass : NSObject <NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

(Note *)createNote;
(BOOL)deleteNote;

I think, if one day, I don't want to use core data to store my project, I can replace it with low cost.

All above, is that right? and can I do this?

Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63

2 Answers2

0

Yes, it's a good idea to separate your data storage code from your user interface code. I tend to use a singleton class like this to manage data for an application.

Community
  • 1
  • 1
Jim
  • 72,985
  • 14
  • 101
  • 108
0

Its better to factor that out into a data access object. The code isn't spread around, easy testable (code coverage) and reusable even in other projects. Using a singleton and putting that into the appDelegate makes sense in most cases.

hburde
  • 1,441
  • 11
  • 6