0

Take NSURLSession's task objects:

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];

NSURLSessionDataTask *dataTask1 = [session dataTaskWithURL:[NSURL URLWithString:@"http://i.imgur.com/RARAP1J.jpg"]];

If I was in the following delegate method:

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

If I ended up creating a bunch of NSURLSessionDataTasks, how would I go about checking which one dataTask refers to. Sure, I could have a bunch of properties, but if I have 50 tasks, that gets unruly and I'd like to be able to automatically set it up in code.

I know of the taskIdentifier method, which would work, but it's readonly and I want to be able to set it.

1 Answers1

0

You can put any string you like in NSURLSessionTask.description. I even use NSJSONSerialization to stuff NSDictionarys there. (Ask in the comments, and I'll post the code). I can verify that this survives the backgrounding serialization of tasks.

You could also just keep a NSMutableDictionary ivar in your class, mapping taskIdentifier => arbitrary info, and removing the entry when the task is complete.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118