Deep Breakpoint Continuation of Nsurlsessiondownloadtask

Source: Internet
Author: User
Tags tmp file tmp folder

Http://www.cocoachina.com/ios/20160503/16053.html

This article is a contribution article, Weitchen

For background downloads and breakpoints continue to believe that you will not be unfamiliar, then if you want to achieve both of these requirements how to do?

The use of nsurlsessiondatatask can easily achieve the continuation of the breakpoint, but there is a fatal drawback is that the background can not be downloaded, a little application into the background, will stop the download. That's why we can't meet our needs. And Nsurlsessiondownloadtask is the only class that can be downloaded in the background, so we can only do it from this class.

There are a lot of data about nsurlsessiondownloadtask on the internet, but it is very regrettable that the basic CV Dafa is identical. And only one pause button resumes downloading, and the continuation of the breakpoint after the application is closed is completely blank.
So in this article we'll talk about how to do a breakpoint continuation if the application is likely to be killed at any time.

About the continuation of the breakpoint principle:

First of all, if you want to make a continuation of the breakpoint, then you need a simple understanding of the continuation of the breakpoint work mechanism, in the HTTP request header, there is a range keyword, through this keyword can tell the server to return what data to me.
Like what:
bytes=500-999 = No. 500-No. 999 byte
bytes=500-represents all bytes from the NO. 500 byte backwards
Then, according to the data returned by the server, the resulting data is stitched to the back of the file, so that the breakpoint can be resumed.

About Nsurlsessiondownloadtask Basics

You can refer to this article: iOS using nsurlsession for file breakpoint download

Analysis of File download and pause

When using Nsurlsessiondownloadtask to download, the system will create a download path under the cache folder, there will be a "cfnetworking" under the path of a. tmp file (hereinafter referred to as "download file" to prevent confusion), This is the file that we are downloading. And when we call the cancelByProducingResumeData : method, we get a data file, formatted with a string, and found to be an XML file that contains a description of some key points about the. tmp file, including "Range", "key", "path to download File" And so on. The downloaded file that existed in the download file is moved to the System TMP folder directory. And when we do this again resume , the download file is moved back to the download folder.

About the program was killed the breakpoint continued to pass Resumedata

According to the above analysis, the following conclusions can be obtained:
1.DownloadTask each time a breakpoint is resumed, according to the data file "path key" to find the download file, and then check and then according to the "Range" property to continue the breakpoint.
The 2.download folder will only be stored in the downloaded file, and once paused, it will be moved to the TMP folder.
3. Each paused data file corresponds to the download file one by one.
3. The continuation of a breakpoint is only related to files in the TMP folder.

Specific implementation

To save performance, I tried to find the callback before the program was killed, but it was a pity that I failed because I couldn't control where I was going to save my progress, so I could only save it once every once in a while. Setting a bool variable is used to determine if it is being downloaded, while pausing at a periodic event (sounds silly, but it seems to be the only way to get the data file). Then save the data file and copy the downloaded file under the TMP folder to the secure directory (because the TMP folder is said to be empty at any time).
When downloading again, first from the security directory to download the file, delete the TMP folder has the same name file, and then copy to the TMP directory, and finally use the saved data file for re- downloadTaskWithResumeData operation, you can realize the download again.

Pros and cons

Benefits:
1.DownloadTask can be downloaded in the background without having to keep the app in the foreground, the user experience is good.
2. After the process has been killed at any point in time, the breakpoint can still be resumed.

Defects:
1. Because Apple does not provide a good API, so there will be a loop check, every time pause for a second or so, the efficiency is slightly reduced.
2. If the set save interval is too long, the middle kill process may lose more progress.

Finally, we enclose the demo's GitHub address: Https://github.com/WeiTChen/NSURLSessionDownload.git

Deep Breakpoint Continuation of Nsurlsessiondownloadtask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.