Isuru
Isuru

Reputation: 31323

Not sure where the core data code should go in the MVC

In this app I'm working on, I use core data to store content. I populate the database in the AppDelegate's didFinishLaunchingWithOptionsmethod. And in the view controller, I'm extracting the required data using a NSFetchRequest.

My question is, is the code related to extracting data should be in the view controller? Or should I create separate methods in the NSManagedObject subclasses generated for those entities and call them from the view controller?

Can someone please shed some light on this?

Thank you.

Upvotes: 2

Views: 340

Answers (1)

Maciej Oczko
Maciej Oczko

Reputation: 1225

You should create a model class, which means something like:

@interface DataImporter : NSObject
@property(nonatomic, readonly) CoreDataManager *coreDataManager;

 - (id)initWithCoreDataManager:(CoreDataManager *)coreDataManager;

 - (void)importDataWithCompletionCallback:(void (^)())completionBlock;
@end

It's not a NSManagedObject subclass, but it's an object responsible for importing data into your CoreData store. importDataWithCompletionCallback can e.g. create all necessary NSManagendObject objects (depending on some JSON) and save them, do it in background, call callback block after completion.

Subclassing NSManagendObject is not a good way to follow.

Upvotes: 1

Related Questions