ios - Download files one by one using NSURLSessionDownloadTask -
In my app I get JSON from the web service. This is the URL of many files in JSON which I would like to download.
I want to download each file one after the other (wait for the first download until the second is started and goes further and further) NSURLSessionDownloadTask Using. I would also like to keep track of total bytes so that I can update the UI.
Thanks a long time ago! Like the downloadtosk, you know that their equivalent NSURQ (where it can be explained within NSOction).
Unlike NSOperationQueues do not play very well with.
An option will add all your URLs to an array, and then within the closing handler of the work, just line the next item.
Then you can create a single loop in your works, each task completion of the handler inside a progress shop, store the work in an array, and the next task queue inside the handler of each task completion: / P>
- (zero) addRequestsWithURLs: (NSArray *) URL progressBlock: (zero (^) (NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations, NSURLSessionDownloadTask * function, NSURL * location, NSURLResponse * response, NSError * error) ) ProgressBlock {__block NSUInteger numberOfFinishedOperations = 0; NSUInteger totalNumberOfOperations = [urls count]; (For NSString * url in URL {NSMutableURLRequest * request = [URL with NSMutableURL request request: [NSURL URLWithString: url]]; __block NSURLSessionDownloadTask * Tasks = [self.session downloadTaskWithRequest: request completionHandler: ^ (NSURL * location, NSURLResponse * response, NSError * error) {// downloadFileSomewhere ++ numberOfFinishedOperations; If (progress block) {progress block (number final completions, total numberof operation, work, destination! = Zero? [Nsur file URLWithPath: destination]: zero, response, error); } // queue next [self-process fully Task: work]; }]; // NSURLSessionTasks stores an array of [self.tasksWaitingToBeQueued addObject: task]; }} - Tasks containing the (zero) process: (NSURLSessionTask *) Completed Task {// Clean and next line [Auto. Tasks. Waiting. QAd removed object: fullscache]; NextTask = [self.tasksWaitingToBeQueued first object]; If (NextTask) {[Resume Next Task]; }} note
In this example I show progress as the number of work and not the number of bytes, it is recommended In order to indicate progress by using the viewpoint (its easy) byte, you must first know the total number of bytes (because you want to show a progress bar) to download and implement the NSRSL representative and the progress of each task Of the Download the queen, bytes and update your block. If your server does not tell you the total number of bytes, then you probably have to make a head request for every resource and make the sizes the only one. Personally this solution can be complicated in a complicated way, which shows progress as the number of file downloads.
To achieve this, something like this can be seen:
- (zero) URL session: (NSKRS * *) Session download task: (NSURSLSS Download Download Task *) DownloadCakask Deutredadata: (Int 64_T) Bytes Written Total Bits Written: (Int 64_T) Total Betrayed Total Bitex Expected Writing: (Int 64_T) Total Bytes ExactTouch {Self. Taitibets Written + = Total Bytes; NSUInteger Total ProgressSoffers = Self. Written Totlabytes; NSUInteger Total estimated bits = self.totalExpectedBytes; // You will need to locally capture some progress block -. Maintain the chakras self.progressBlock (totalProgressSoFar / totalExpectedBytes)} If you finish the progressBlock should be set to zero when
Comments
Post a Comment