3 Replies
      Latest reply on May 25, 2016 1:06 AM by eskimo
      alit Level 1 Level 1 (0 points)

        I wrote an app which uses NSURLSessionDownloadTask in a background NSURLSession to download a portion of a big file, for example 100MB out of a 1GB file, by setting HTTP Range header in NSMutableURLRequest.

        The task is created correctly and countOfBytesExpectedToReceive is correct and represents the requested range and the task would be completed usually but sometimes in an unknown circumstances (I guess occasionally when a task is moved to a new session with same background identifier), from a point onward the Range header will be discarded and task would begin to download entire file, albeit corrupted, and countOfBytesExpectedToReceive would be reset to the size of entire file.

        I don't know whether it's my fault or it's a problem within framework itself, but I hope anybody here can help me to solve this problem.

        • Re: NSURLSessionDownloadTask bug/feature on requests with Range header
          eskimo Apple Staff Apple Staff (12,035 points)

          An NSURLSession background session can automatically resume failed downloads.  It does this using ETags and Range headers as described in QA1761 Resumable Downloads.  This is a neat-o feature but, alas, it has a bug (r. 22555933).  Specifically, the resume code does not take into account any Range header that you might have applied to the original request.  The end result is that it’s not safe to use a Range header in an NSURLSession background session download task.

          This makes me sad )-:

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: NSURLSessionDownloadTask bug/feature on requests with Range header
              alit Level 1 Level 1 (0 points)

              Thank you for information, please help me on this questions I have:

              1. Is there any notification we can take into account to get resume data of downloaded part before it destorys downloaded data? or call to delegate methods?

              2. is there a key to change this behaviour of resuming failed task?

              3. Is there a plan to fix this issue in next release of iOS or in current release?

                • Re: NSURLSessionDownloadTask bug/feature on requests with Range header
                  eskimo Apple Staff Apple Staff (12,035 points)

                  1. Is there any notification we can take into account to get resume data of downloaded part before it destorys downloaded data? or call to delegate methods?

                  No.  The problem is that your app might be suspended (or even terminated) when the resume happens, so a notification isn’t possible.

                  2. is there a key to change this behaviour of resuming failed task?

                  Not directly.

                  One option would be to tweak your request so that it’s not resumable.  I posted on the old DevForums as to the criteria that NSURLSession background sessions apply when deciding whether to allow a resume.  The drawback with this is … well … that you don’t get resume, which is kinda important.

                  If you have control over the server the best way to fix this problem would be to have the server publish the resource’s segments as separate resources.  So, if the resource is <http://example.com/HugeFile.db, you could have the server allow you to access segments as <http://example.com/HugeFile.db/seg1, <http://example.com/HugeFile.db/seg2, and so on.

                  3. Is there a plan to fix this issue in next release of iOS or in current release?

                  I can’t comment on The Future™.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"