NSUrlSession pauses and resumes device lock release

I upload a file to the server using multipart data with NSURLSession . When the application is running in the background, I want to pause the request and resume when the application reappears in the foreground. So I just do [session suspend] and [session resume] . This works fine when the application exits only in the background. But if the device locks up when you return to the foreground and try to resume work, I get a network connection error message. I understand that when the device is locked, all open sockets are closed and therefore a problem, but is there a way to do this without having to start the download from the very beginning?

+5
source share
2 answers

You must switch the foreground session to the background session before the application goes into the background, and then there is no need to pause it. Your file will be loaded by the OS in the background (in the end).

+2
source

Unfortunately, according to the documentation, you need to use a file to do background loading.

In the "Background Transfer Matters" section:

Only downloads of tasks from a file are supported (loading from data objects or a flow failure after the program exits).

(In addition, there is no guarantee when and why your application will be terminated. Trying to avoid blocking the device will not be enough, there are many other ways in which your application may be terminated.)

0
source

Source: https://habr.com/ru/post/1264336/


All Articles