NSURLSessionDownloadDelegate and NSURLSessionDownloadTask are not consistent as previous iOS versions

NSURLSessionDownloadTasks are having issue when resumed more than once.


Pausing and Resuming a DownloadTask by calling

- cancelByProducingResumeData: and - downloadTaskWithResumeData:
twice, will fail a download task.


By observing NSURLSessionDownloadDelegate, we found that "didResumeAtOffset" and "didWriteData" behave differently than on previous iOS versions.

For example, when we are downloading a 100M file:


fileOffset(in didResumeAtOffset) expectedTotalBytes(on iOS 11) expectedTotalBytes(on iOS 10-)

1st Resume 15M 100M 100M

2nd Resume 28M 85M(100M-15M) 100M

3rd Resume 39M 57M(100M-15M-28M) 100M


This wrong calculation will cause the download job finished earlier since for example, in the 2nd case above, the system claims the the download is completed when it hits 85M but actually it still has 15M to go. Corresponding delegate methods( didFinishDownloadingToURL and didCompleteWithError) are called in such case. And the whole process is completely out of users' control.

Replies

Download task is completed, but the file is corrupted since the job is not done(part of the file is still missing)

Try remove object for key "NSURLSessionResumeByteRange" in your resume data after calling cancelByProducingResumeData

I 'm experiencing the exact same problem in the official release of macOS 10.13.

@Navtech Did you file a bug report?


The tip of Sina Hadipour works, strangely enough. It seems that if key NSURLSessionResumeByteRange is removed, the resume range is recalculated based on NSURLSessionResumeBytesReceived.

I meet same problem as yours.This is my test demo, https://github.com/bqlin/NSURLSessionDownloadTask-Experiment.

Seems to be working in iOS 11.2 again.