2

I used to do what I wanted with NSURLConnection.

  • I would create a new custom NSOperation object
  • setup my NSMutalbeRequest and when ready trigger the operation witch would queue the operation...
  • in there I would have the NSURLConnection delegate methods and with that I could track all progress, completion, errors etc...

So, the point is, request would be contained in its own operation and the delegate calls would happen in there, having a 1-to-1 relationship between the request operation object and the NSURLConnection delegate.

I am not sure HOW to do this with a NSURLSession. With the NSURLSession I can trigger all the dataTasks I want for all the GET calls I want to perform. However I setup the shared session using this call:

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;

So the problem I am trying to solve is how can I track multiple GET requests progress if I have only one object where all the delegate calls happen? I only have ONE DELEGATE in there. So now I have 1 session, 1 delegate object but I have multiple GET requests happening at the same time.

I know each task has a "taskIdentifier", but that requires me to have some kind of dictionary.

For example when the delegate call:

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data

is called, then I'd have to look up the task in a dictionary and then the object I'd get from the dictionary could have the reference to what I need to track the progress of that one request...

I am not sure what the best way to solve this problem is? Does anybody know how this should be done?

Wain
  • 118,658
  • 15
  • 128
  • 151
zumzum
  • 17,984
  • 26
  • 111
  • 172
  • Your dictionary solution sounds ok. What kind of tracking do you need / situation do you think will be a problem? – Wain Feb 23 '14 at 19:35
  • The way you said is exactly what you have to do, since each data task needs to append to its own mutable data. – matt Feb 23 '14 at 19:36
  • possible duplicate of [How do I get the data from a finished \`NSURLSessionDataTask\`?](http://stackoverflow.com/questions/21924483/how-do-i-get-the-data-from-a-finished-nsurlsessiondatatask) – matt Feb 23 '14 at 19:38
  • @matt: that link you posted is more towards how to get data out of the task.... I know how to do that, I am more concerned on how to structure things to manage multiple concurrent requests from the same delegate through which all the requests go through... – zumzum Feb 24 '14 at 00:59
  • No, look at Rob's comprehensive answer. – matt Feb 24 '14 at 01:03
  • The way I've seen this done is using objc_setAssociatedObject to staple a subsidiary object to the task, but I'm not sure that's any cleaner than a dictionary. – Matt Gibson Jun 26 '14 at 13:50
  • Why not just create a new session for each GET, which then maps easily to your old method with NSURLConnection? What is the advantage of sharing a single session? – user1055568 Jan 09 '16 at 16:05

3 Answers3

0

Basically what you need to do is set accessibility label for each requests

    for task 1

    [dataTask setAccessibilityLabel:@"label1"];

    for task 2

    [dataTask setAccessibilityLabel:@"label2"];

and then in delegate methods check for accessibility label

    if([dataTask.accessibilityLabel  isEqual: @"label1"])
     {
        // handle data from first task

     }

     if([dataTask.accessibilityLabel  isEqual: @"label2"])
     {
       // handle data from second task

     }
akash300
  • 49
  • 1
  • 8
0

You could also instantiate the NSURLSessionDataTask with your own subclass of NSURLRequest which you can retrieve from task.originalRequest in the session delegate.

user1055568
  • 1,349
  • 13
  • 21
  • I liked this idea and it works great for NSURLRequest. However, I tried same idea for an NSMutableURLRequest and that failed. After casting task.originalRequest to my subclass, it aborted with unknown method on NSMutableURLRequest. – user1055568 Nov 20 '16 at 19:44
-2

So,

what worked best for me so far is using the NSDictionary approach. That allows the NSURLSession delegates to get a hold of the right data task and handle multiple requests at once.

I am not sure if this is the best approach, but my app is now running lots of requests and things seems to work fine.

zumzum
  • 17,984
  • 26
  • 111
  • 172