Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do NSURLConnections belong in my model or controller?

Say I'm using a JSON or XML API to get data about my projects from a URL using an asyncronous NSURLConnection, parsing that into an NSMutableArray and then populating an NSTableView.

I have a model: Project I have a controller: TableViewController (acting as table data source and delegate)

Where should I put the code that starts the request and parses the result into a NSMutableArray.

Should I have:

1:

A method inside Project called -(NSMutableArray* ) getAllProjects and call this from my Controller.

Or 2:

Should I enumerate an NSMutableArray of Project* objects, called for instance ProjectsArray* inside my Controller; each time calling [[Project alloc] init]?

Option 1 makes much more sense to me because I might want to get all the projects from multiple controllers and this would save repeating code, I only need to call a public method inside my Project model. In this case would I be doing lots of [[self alloc] init] statements? Is this ok? Also my model would need to be an NSURLConnection delegate. Is this correct?

like image 279
Matt Harrison Avatar asked Oct 23 '22 03:10

Matt Harrison


2 Answers

No doubt it must be in your Model.

Reason :

Because you will be requiring to update it many times from different controllers, you can use KVO for that in future.

like image 94
Anoop Vaidya Avatar answered Oct 30 '22 20:10

Anoop Vaidya


From my experience, I think the good way is to have the parsing routines in the model (ProjectsArray) and connection stuff in another class, which initiates the connection and returns raw NSData (through a delegate for example), which you pass to the model to parse it. This way your model or viewController won't have multiple roles.

As for calling [[Project alloc] init] each time you need the data—you can use static reference in the model class and then getting it by something like - (ProjectsArray *)instance

like image 32
Michael Ruml Avatar answered Oct 30 '22 18:10

Michael Ruml