简体   繁体   English

异步下载与同步下载多个文件

[英]Downloading Multiple Files Asynchronously vs. Synchronously

I have an iOS app in which I need to download multiple audio files before a player can start. 我有一个iOS应用程序,需要在其中启动播放器之前下载多个音频文件。 (All the files need to be downloaded first because they all play simultaneously as a multi-track song.) (需要首先下载所有文件,因为它们都作为多轨歌曲同时播放。)

I know about the advantages of downloading asynchronously from the main thread (not blocking the UI, etc.) but I'm wondering if there's any advantage to downloading each of the files asynchronously from each other, vs. all on the same background thread. 我知道从主线程异步下载的优点(不阻塞UI等),但是我想知道,与在同一后台线程上异步下载每个文件是否有任何好处。 Which approach would download all the files fastest, if there's a difference? 如果有区别,哪种方法将最快下载所有文件?

It's really a matter of network bandwidth. 这实际上是网络带宽的问题。 Most likely if you tried to download 10 files at the same time, it will take as long (and possibly longer) than downloading the same 10 files one at a time. 如果您尝试同时下载10个文件,则很可能比一次下载相同的10个文件所花的时间更长(甚至更长)。

A user's Internet connection only allows so much data per second. 用户的Internet连接每秒仅允许这么多的数据。 Assuming that is maxed out for each download, downloading more than one file at a time means that the max throughput has to be split between the files. 假设每次下载都已最大化,则一次下载多个文件意味着必须在文件之间分配最大吞吐量。

Your best option is to setup a concurrent operation queue. 最好的选择是设置并发操作队列。 Queue each download as a separate operation. 将每个下载排队作为单独的操作。 Then experiment by setting up the operation queue to support anywhere from 1 to n concurrent operations. 然后通过设置操作队列以支持1到n个并发操作的任意位置进行实验。 Do the tests multiple times at different times and track how long it takes to complete all of the downloads. 在不同的时间多次进行测试,并跟踪完成所有下载所需的时间。 See which results in the best overall average. 查看哪种结果可获得最佳的总体平均水平。 Keep in mind the results could be different for a user on a slow 2G cellular connection vs. someone on a super fast home Wi-Fi connection. 请记住,使用2G蜂窝网络连接速度较慢的用户与使用超快速家庭Wi-Fi连接网络的用户所得到的结果可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM