Is there any difference between dataWithContentsOfURL (threaded) and dataTaskWithURL?

后端 未结 2 1639
天涯浪人
天涯浪人 2021-01-24 15:25

We\'re using dataWithContentsOfURL because it is, uh, simple...

NSData *datRaw = [NSData dataWithContentsOfURL:ur];

Now, of course, that will h

相关标签:
2条回答
  • 2021-01-24 15:54

    One reason to prefer true async io over threaded synchronous io is that threads are not free memory-wise. It's not a huge deal in general but you can save a little memory in your app and (more importantly) a little wired memory in the OS's kernel by not keeping a thread sitting around doing nothing while it waits.

    0 讨论(0)
  • 2021-01-24 15:56

    Some of reasons I can see:

    • With synchronous request you can't know download progress and can't resume download. If you download big file and it fails on 99%, you will need to redownload whole file.

    • As Apple states "Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network...". If you are using GCD, you won't directly control the thread you are given and it may block some other important operations on that thread, dataTask scheduler may have better overview of system resources. If you create thread manually, you may overload system(if there is already resource strain at least) with this blocked thread.

    • Also there is "added ability to support custom authentication and cancellation" in dataTaskWithURL:.

    • You may need to customize request headers/body. Maybe it falls in "convenience" category, but anyway it's another thing.

    0 讨论(0)
提交回复
热议问题