Danie1s / Tiercel

简单易用、功能丰富的纯 Swift 下载框架
MIT License
2.67k stars 388 forks source link

后台下载任务完成后,队列里等待中的任务没有开始执行。 #128

Closed GloamZz closed 4 years ago

GloamZz commented 4 years ago

我使用的系统是iOS13.6(iPad Pro 2nd),测试了好几次,后台下载任务调度好像存在问题。 已经开始下载的任务在后台下载完成之后,队列里的其他任务不能紧接着上一个任务的完成继续创建并执行下载,创建的新downloadTask调用了resume()却并没有执行请求。 这个会跟下载服务器有关系吗,我测试使用的URL是局域网内的cdn下载连接。 抓包发现进行中的后台任务的结束FIN ACK包后之后就停止了,没有新的SYN包发送。

Danie1s commented 4 years ago

兄弟,我刚测试了,没有问题,只是新开启的任务并不会马上开始下载。请看下面这段官方文档的说明

Use Background Sessions Efficiently

When the system resumes or relaunches your app, it uses a rate limiter to prevent abuse of background downloads. When your app starts a new download task while in the background, the task doesn't begin until the delay expires. The delay increases each time the system resumes or relaunches your app.

As a result, if your app starts a single background download, gets resumed when the download completes, and then starts a new download, it will greatly increase the delay. Instead, use a small number of background sessions — ideally just one — and use these sessions to start many download tasks at once. This allows the system to perform multiple downloads at once, and resume your app when they have completed.

Keep in mind, though, that each task has its own overhead. If you find you need to launch thousands of download tasks, change your design to perform fewer, larger transfers.

Note

The delay is reset to 0 whenever the user brings your app to the foreground. It also resets if the delay period elapses without the system resuming or relaunching your app.

简单来说,就是如果下载任务是在后台开启的话,会存在一个延迟,而且这个延迟会越来越高,直到 App 重新进入前台才会重置

GloamZz commented 4 years ago

多谢,了解了。我们设计的下载方式还是不合理,我们为了资源更新更加的颗粒化,把一整个下载任务分成了更多的小部件进行下载,导致了会存在的更加多的下载任务,按照这样的延迟越来越高看来是不可取的。再次感谢。