0

I'm creating a service class in Objective-C that communicates with a HTTP server. I'm familiar with NSURLConnection and the associated classes to make an asynchronous request to a server. So far, I've used self as the delegate, responding to the four delegate methods (didReceiveResponse, didReceiveData etc.) required for the call.

The service class I'm building must be able to handle several requests at once, and respond differently to each. I'm looking for a way to achieve this, without an endless switch or if-elseif in the connectionDidFinishLoading method.

I have the idea to create a delegate class that implements the four methods described above, and give that to the NSURLConnection object to talk to. However, I want to notify the parent class when the connection finishes loading and then fire another method within the parent class. And of course, the delegate doesn't know which method this is - it could be anything.

How to proceed? Is there a way to set a selector for the delegate class, and fire that selector (which is a method) when the request is finished? How do I do such a thing?

(Creating a delegate for my delegate, and then calling a superclass method could do the trick, but I'm really looking into wildcard methods, such as a selector.)

Any help is greatly appreciated!

Regards,

Reinder

Keavon
  • 6,837
  • 9
  • 51
  • 79
Reinder de Vries
  • 522
  • 1
  • 6
  • 26

2 Answers2

0

You have three options:

  1. Using a block callback
  2. Using notifications
  3. Using a delegate, as you already suggested.

All three are perfectly valid, but with various advantages/disadvantages. Learn them all, then decide which to use in each scenario. :-)

Often more than one solution will be chosen. For example many of Apple's classes implement a delegate method and send a notification for the same event.

Community
  • 1
  • 1
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
0

I would pass a block (^{ ... })to the delegate that it should call when the connection has finished.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256