To look at this in accordance with MVC, you have two parts:
- An array is a model
- The code that retrieves the inventory from the server is the controller
This controller code is indeed a model controller, not a view controller. I would not put it in a view controller class. You can put it in your application delegate if the code is very simple, but I would recommend it completely in my class.
In -applicationDidFinishLaunching :
[[InventoryController sharedInstance] reloadContent]; [[InventoryController sharedInstance] scheduleUpdates];
InventoryController.h
@interface InventoryController @property (retain) NSArray *inventory; @property (retain) NSTimer *reloadTimer; + (InventoryController *) sharedInstance; - (void) reloadContent; - (void) scheduleUpdates; @end
InventoryController.m
@implmentation InventoryController - (void) reloadContent { ... } + (InventoryController *) sharedInstance { static InventoryController * singleton; if (!singleton) singleton = [[self.class alloc] init]; return singleton; } - (void) scheduleUpdates { self.reloadTimer = ...; } @end
Everywhere:
NSArray *inventory = [[InventoryController sharedInstance] inventory];
In -reloadContent you have to pull the content from the server. In -scheduleUpdates you must configure a timer that acts on the controller, causing it to periodically reload data. If your view controller needs to adjust its behavior when the data is out of date, save the NSDate next to the array, add a method like -isStale , which checks the dates and is called first.
Remember to download the urls in the background. You should not stop and reload data during the processing of an event or action, so essentially your view controller should return from the action method while waiting for the data and configure its display when you return the data.
If the view manager should respond after updating the data, register the view controllers for a notification that you can post when the inventory controller completes updating its content:
[[NSNotificationCenter defaultCenter] postNotificationName:InventoryControllerDidReloadContent object:self];
If you want to cache the inventory data on the device so that you can delete it from memory when the application goes into the background, you can do this by writing the array to the property list, but if the obsolete data is not useful, you can not worry.
You can use Core Data instead of an array and a list of properties, but it does not eliminate the need for a controller that downloads data from the server and loads it into context. Instead of having an array, you probably have the context of the managed entity and the resulting result controller. If you do not edit this content in your application, I doubt that Core Data will provide you any advantages over the list of arrays and properties.