Reputation: 820
My application downloads photos from web server. Is downloading photo information from server Controller's business or data's business? I mean should I download data in controller class
Photo.title = [self getPhotoInfoFromServer: url];
or in data class?
[Photo getPhotoInfoFromServer: url];
Upvotes: 2
Views: 167
Reputation: 17054
An UIViewController is here to Control a View. It means interactions (give good data to the view, and answer to view interactions).
A model / an entity can be created by independents sources (unless the source itself is semantically linked with your model). A Photo is a Photo, whatever if created from a data retrieved from a server or from a local Camera Roll picture.
You should use a custom DataFetcherController to get a remote data and transform it into a instance.
Your UIViewController subclass
needs a list of photos. It asks the PhotoServerFetcherController
for getting the data from the Server. The fetcher can look into its cache and perfom a request the a server if needed. When the PhotoServerFetcherController
get the data and parse it to instances of your Photo
entity and give it back to the UIViewController`.
The best is to create your own FetcherController
interface (@protocol
keyword). You can handle 2 async callbacks : success
with the data in it and failure
with the error in it (connection ? bad input ?).
Here you have many ways to implements that, the main 2 patterns for 1 to 1 interactions are delegate
or block callbacks
.
Upvotes: 1
Reputation: 4044
I prefer using a dedicated server class, which instantiates models, and is used from the controller: [photoServer getPhotoFromURL:URL]
, or [photoServer getPhotoFromURL:URL completion:^(Photo *photo, NSError *error){...}]
Why a dedicated server class?
+[Photo getPhotoFrom...]
methods and hinder the reusability and maintainability of your Photo class.Why should this code be called from the controller?
Upvotes: 1
Reputation: 3918
You should download the photos in to data class then the viewControllers those needs that data can easily get from data class. Actually you should follow MVC where
M= Modal (Your Data Class)
C= Controller class
V= view what your controller displays
Upvotes: 1