0

Sorry for noobness-level of the question.

I saw this answer, where it says

Add something like this to your NSURLConnection delegate

However, the method in question, sendSynchronousRequest:returningResponse:error:, is a class method. If I understand delegate methods correctly, they use a delegate which is specified when the NSURLConnection object is initiated.

So, if I have no instance of the class, a delegate can't be used. Did I understand correctly?

Community
  • 1
  • 1
Ovesh
  • 5,209
  • 11
  • 53
  • 73

2 Answers2

1

Yes delegates are used for handling events when you send your request asynchronously.

While sending synchronous request using +sendSynchronousRequest:returningResponse:error: method delegates are not used - you only get the resulting data and (optionally) server response and error and not aware about intermediate loading events (and remember that calling thread is also blocked while connection is in progress).

Vladimir
  • 170,431
  • 36
  • 387
  • 313
1

Yes, you are correct. In Cocoa, assigning a delegate requires an instance. In the case of NSURLConnection, its delegate is supposed to be used when making an asynchronous request that is initiated with -initWithRequest:delegate: or +connectionWithRequest:delegate:

Since classes are also objects it could be conceived that they also could have delegates. However, since there is no concept of class variables in Objective-C, a class would have nowhere to store its delegate (it could use a standard C variable in its .m file, though). Anyway, this concept is not used in Cocoa.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Does that mean I need to call sendSynchronousRequest:returningResponse:error: on an instance? Would that even work? Or am I forced to work with an async request? – Ovesh Dec 16 '10 at 14:05
  • 1
    No, `+sendSynchronousRequest:returningResponse:error:` is a class method. You can't call it on an instance. That method does not work with a delegate. You just call it and it returns a response when it's ready (could be very fast, could take 30 seconds). If you want to use the delegate methods to be informed about the connection's progress, you need to create an asynchronous request. – Ole Begemann Dec 16 '10 at 14:20
  • So, the solution suggested in the answer I linked to cannot be used directly. The implementation needs to be changed to use an async request, and only then the solution can be used. Please correct me if I'm wrong. – Ovesh Dec 16 '10 at 14:50