NSPOSIXERROR Code=2 on resume NSUrlSessionDownloadTask

Hi All,


I'm working on the download infrastrture of my company's app.

In the app, the user can download videos to watch offline and these are large downloads (>~1GB).

Beacuse of this, we want our download experience to be as smooth as it can be.


There is one scenario I'm really having trouble with:

If the user kills the app while downloading, the downloads stop.

On the next luanch we call getTasksWithCompletionHandler to catch all the lost download task.

We create a new task from the old one's reasumeData and resume it immediately.

like so:

if (task.error && task.error.code == -999 && task.error.userInfo[NSURLSessionDownloadTaskResumeData] != nil) {
   task = [urlSession downloadTaskWithResumeData:task.error.userInfo[NSURLSessionDownloadTaskResumeData]];
   [task resume];
}

The new task will end with immediately with the error: NSPOSIXERROR Code=2


This happend on both the simulator and on a real iphone, both running iOS 11.


Please help me, I have no idea what to do.

Thank you.

Error code 2 is

ENOENT
, which is usually associated with a file being missing. I don’t know the exact cause of this but I do have a question about how you’re debugging this. Are you relaunching the app from Xcode or from the Home screen?

Launching your app from Xcode can result in some weird artefacts like this, ones that don’t show up in production. See my Testing Background Session Code pinned post for a specific recommendation on how you should test background session code.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I was going to write you how it happens even without xcode but then it stopped happen.

I will release a staging build of the app and we'll see if this issue exists in a non-dev build.


Thank you.

NSPOSIXERROR Code=2 on resume NSUrlSessionDownloadTask
 
 
Q