1

If I would recreate NSURLSession, would I be copying the block parameter passed into this function: -

- dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;

To further illustrate, if I am writing NSURLSession, following ARC/Blocks best practices, should I copy the completionHandler passed into the above function, as they get called asynchronously (maybe after the stack has popped?).

aprofromindia
  • 1,111
  • 1
  • 13
  • 27

1 Answers1

1

NSURLSession will copy those blocks itself.

So, no, you do not need to copy them. Note that if you elected to call copy on the completion handler, unless you yourself kept a reference to the copy, ARC would simply release the copy after you had "finished" with it, which would be immediately after the copy had been passed into the dataTaskWithRequest:completionHandler: function.

Also, there is only one completion handler passed into that method.

Ben Pious
  • 4,765
  • 2
  • 22
  • 34
  • Right, so if I am writing my own NSURLSession, I should copy the block parameter passed ? – aprofromindia Aug 09 '15 at 19:15
  • Yes. It would be exceedingly poor design to write a class that required the user to manage its resources. Also, I think it would be difficult to avoid doing so just as a consequence of keeping the block addressable -- all class properties that take a block are `copy`, and I think adding it to an `NSArray` or `NSDictionary` will either implicitly copy the block, or refuse to compile until you do. – Ben Pious Aug 09 '15 at 19:19