Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ZaBlanc

macrumors newbie
Original poster
Apr 28, 2009
10
0
I'm a one-month veteran of iPhone development, and I have a design question regarding NSURLConnection and, well, maybe I'm missing something.

From what I can see, the way you do async network connections is by setting a delegate on the NSURLConnection. Is it me, or is this limiting? Here's what I mean...the delegate is an instance of an object that defines a set of 4 functions to handle successful, and failed, data retrieval.

This means I have to do one of two things:

* Create one class that uses if/else statements to handle responses based on the connection parameter.
* Create multiple classes, one for each HTTP call, to handle responses.

Perhaps this is a design discussion, but how are you guys handling multiple calls to a server? Do you create multiple delegates? In the Flex world, I could use one big class that has small handlers for each call by specifying different methods to call back on response.

I'm interested to know how you guys organize your class structure for handling this scenario...?
 
It depends entirely on how you're communicating with your server. What I usually do is work with a basic Downloader class which itself manages the NSURLConnection stuff and provides a simpler loading/callback mechanism for downloading from a URL. Then, I create another class that is designed to work with a certain type of server/format. This class would handle sending (multiple, if necessary) requests to the server (using the Downloader class) and parsing its results, and then passing back its parsed content (as normal NS* objects) back to its delegate.

Hope this helps (and makes sense).
 
Events?

How do you handle notification from your class back to, say, a controller? Delegates? Events? Binding?
 
On MacOS X there is a class called NSURLDownload. It's a wrapper around NSURLConnection but it has the ability to download the url contents to a specified file. For some reason that class isn't included on iPhone OS but I needed that functionality so I wrote a duplicate of that class. It has similar delegate methods to NSURLDownload and it writes to a file. If you google NSURLConnection you'll find examples of this kind of class, although many accumulate the downloaded data in an NSMutableData instance, which isn't a good design because the data can get big.

So in this case I have a separate delegate for each NSURLConnection but I have a single delegate for all my Downloader objects.

When you say that the delegate design is limiting do you know that the first parameter to all the delegate callbacks is the NSURLConnection pointer? That means that you can easily set up a single delegate for multiple NSURLConnection objects and do different things for each connection.

In my case I may have multiple downloads running at a time but they are all more or less doing the same thing so I don't really have different handlers for the callbacks, except for writing them to their respective files.
 
I would use delegates. The Downloader class has its own delegate and then the parsing class has its own also.
 
Does any one has any code snapshot where i can send multiple connection to same delegate i got stuck with this . It will be really great if any one can guide me how to get delegate response differently depending upon connection. And also how to compare two connection object.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.