4

I'm watching the Stanford CS193p lecture video. The instructor used both downloadTaskWithRequest: and downloadTaskWithURL: when downloading some photos from Flickr. I'm a bit confused.

I looked up the former in the documentation, which speaks of the NSURLRequest as "An NSURLRequest object that provides the URL, cache policy, request type, body data or body stream, and so on".

I have no idea what "body data" or "body stream" means. It would be fantastic if anyone could help a bit on that, but more important is the problem below.

It seems to me that either method would work just fine according to my experience (which isn't much). I'm intrigued to know what, if any, is the difference between the two, and on which occasions should I pick one over another.

Mr.DDD
  • 43
  • 5
  • 1
    I don't have a full answer but, as an example, I need to request byte ranges from a server. My request is a NSMutableURLRequest, which lets me add headers that include the 'Range' I need to download. – Phillip Mills Jul 08 '14 at 15:23
  • To Phillip Mills: Thanks for your example. It's really helpful. Now I'm much clearer as to when I should use which. – Mr.DDD Jul 08 '14 at 15:42
  • To reply directly to a person's comment, use an @ sign before their name. For example, to reply directly to me, you'd begin your comment with '@duci9y …". – duci9y Jul 08 '14 at 15:50
  • @duci9y Huge thanks! People are really helpful here! – Mr.DDD Jul 08 '14 at 15:54
  • Our pleasure. :) Welcome to StackOverflow. – duci9y Jul 08 '14 at 15:55

1 Answers1

2

If you use the NSURLRequest version, all the details you mentioned can be explicitly set by you. If you use the NSURL version then the default values will be used instead. The default values will cover the majority of cases, but not everything - it really depends on what you're doing.

The body data / body stream (where a stream is a source of data) is some piece of information that needs to be sent to the server for it to understand and process the request. By default no data will be sent. Often you will use query parameters in the URL instead of body data, but again, it depends what you're doing as to what API you need to leverage.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • That's really illuminating. Is that the case that it's simply easier to use the NSURL version when no detail of the request needs to be set, not considering the possibility of future changes? – Mr.DDD Jul 08 '14 at 15:38
  • I'm really sorry that I can't vote your answer up. It seems I'm a little known guy that doesn't even have the right to vote. – Mr.DDD Jul 08 '14 at 15:45
  • Yes, just use the URL version if you don't need the power offered by the request version. You can always accept an answer (to a question you asked) even if you can't vote yet :) – Wain Jul 08 '14 at 15:46
  • To Wain: I see :). Still a bit fuzzy about those features since I'm pretty new here. – Mr.DDD Jul 08 '14 at 15:48
  • The features of the request all relate to things you can specify about how to handle the http config and the network connection. Until you need to do something like that you don't need to worry too much. – Wain Jul 08 '14 at 17:22
  • I was referring to features of Stack Overflow (╯_╰). Thanks nevertheless. – Mr.DDD Jul 09 '14 at 01:47
  • Oh I see, haha, well there are still parts of SO that I haven't used and I've been coming for a year now so don't feel bad about that :-) – Wain Jul 09 '14 at 07:23