Big File Download Considerations
- If you do not dump the downloaded files, it will cause a sharp increase in memory consumption, or even run out of memory resources, causing the program to terminate.
- In the file download process usually occurs midway stop situation, if not to do processing, it is necessary to restart the download, waste traffic.
Solutions for large file downloads
Process the downloaded files, write the data to disk (usually in a sandbox), and avoid accumulating data in memory (nsurlconnection download), with every bit of data downloaded
- Implementing write Data using the Nsfilehandle class
- Implementing write Data using the Nsoutputstream class
When the download task terminates, log the location information at the end of the task so that the next time you start the download
Large file Download (nsurlconnection)
Large file Download (nsurlsession)
- Support breakpoint download, automatically record the location of breakpoints when stopping download
- Compliance with Nsurlsessiondownloaddelegate Protocol
- Using nsurlsession to download large files, the downloaded files will be automatically written to the sandbox Temp folder tmp
- After downloading, it is usually necessary to move the downloaded files elsewhere (the data in the TMP folder is deleted periodically), usually in the cache folder
Detailed download steps
Set Download task task as member variable
@property (nonatomic, strong) NSURLSessionDownloadTask *task;
Get Nsurlsession Object
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
Initializing the Download Task task
self.task = [session downloadTaskWithURL:(此处为下载文件路径URL)];
Implementing Proxy methods
/** each time the data is written to a temporary file, the method is called once, usually in this method to get the download Progress */-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask didwritedata: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswritten totalbytesexpectedtowrite: (int64_t) totalbytesexpectedtowrite{Calculate Download ProgressCGFloat progress =1.0 * TOTALBYTESWRITTEN/TOTALBYTESEXPECTEDTOWRITE;}/** the method that is called when the task terminates, typically used for breakpoint download */-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didresumeatoffset: (int64_t) Fileoffset expectedtotalbytes: (int64_t) expectedtotalbytes{Fileoffset: The offset when the download task is aborted}/** is called when an error is encountered, the error parameter can only pass the client's errors */-(void) Urlsession: (Nsurlsession *) Session task: (Nsurlsessiontask *) Task Didcompletewitherror: (Nserror *) error{}/** when the download is complete, you need to cut the file to a folder that can be saved for a long time */-(void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didfinishdownloadingtourl: (nsurl *) Location{//generate file long-term saved path nsstring *file = [nssearchpathfordirectoriesindomains ( Span class= "hljs-built_in" >nscachesdirectory, nsuserdomainmask, yes) Lastobject] Stringbyappendingpathcomponent:downloadtask .response.suggestedfilename]; //get file handle nsfilemanager *filemanager = [ nsfilemanager Defaultmanager]; //through the file handle, cut the file to the long-term saved path of the file [FileManager moveitematurl:location Tourl:[nsurl Fileurlwithpath:file] error:nil];}
Operation Task Status
/**开始/继续下载任务*/[self.task resume];/**暂停下载任务*/[self.task suspend];
OC-16. large File Download