1
votes

In NSURLSession, we add tasks to a session by methods like

  • dataTaskWithRequest:
  • dataTaskWithRequest:completionHandler:

As far as I understand, if we use the one without completionHandler or use nil for completionHandler, self will be used automatically as the delegate and we can expect delegate methods to be called. Do I understand correctly?

Is there possible to specify different delegate (other than self ) for each task?

I checked sessionWithConfiguration:delegate:delegateQueue and I think that delegate is a NSURLSessionDelegate.

1
With more investigation, it seems that sessionWithConfiguration:delegate:delegateQueue is the way to set delegate for all protocols including NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate and NSURLSessionDataDelegatejqyao

1 Answers

2
votes

Answer my own question. We actually can use sessionWithConfiguration:delegate:delegateQueue to setup delegate. According to apple's URL Loading System Programming Guide,

if you need to handle authentication or caching in a nondefault manner, you must provide a delegate that conforms to the session delegate protocol, one or more task delegate protocols, or some combination of these protocols. This delegate serves many purposes