NSURLSession UploadTask suspend is not working

I am trying to upload a file and while doing so when I suspend the upload operation, the method:

- URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:

doesn't excute, which is expected. But still it continues to send the complete file to the server.


I tried uploading ~200MB file, and I suspended the operation around 10%. And when I see the server side, I could see data coming.

Below is the code:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL =[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
NSURL *urlPath=[documentsURL URLByAppendingPathComponent:@"video2.mp4"];
NSURLSessionConfiguration *backgroundSessionConfigObject = [NSURLSessionConfiguration  backgroundSessionConfiguration:@"ud" ];
//backgorundSession is NSURLSession
self.backgroundSession =[NSURLSession sessionWithConfiguration:backgroundSessionConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:5000/upload/"]];
request1.HTTPMethod=@"POST";

uploadTask=[backgroundSession uploadTaskWithRequest:request fromFile:urlPath];
[uploadTask resume];

I am not able to suspend the task. Though it stops the execution of below method. But it still pushes the data to the server.


- URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: 

Accepted Reply

NSURLSession’s suspend and resume support is not really designed to implement a user-level ‘pause the transfer’ feature. Rather, it’s there to allow you to temporarily disable callbacks as part of some sort of concurrency control system. That's because, as you’ve noticed, a suspended task can still be active on the wire; all that the suspend does is prevent it from making progress internally, issuing callbacks, and so on.

If you’re implementing a user-level pause feature, my recommendation is as follows:

  • For downloads, you should call

    -cancelByProducingResumeData:
    and then resume with
    -downloadTaskWithResumeData:
    . This is supported by most servers.
  • For uploads, things get much trickier. Does your server explicitly support the ability to stop and resume the upload?

Share and Enjoy

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

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

Replies

Below is the code:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL =[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
NSURL *urlPath=[documentsURL URLByAppendingPathComponent:@"video2.mp4"];
NSURLSessionConfiguration *backgroundSessionConfigObject = [NSURLSessionConfiguration  backgroundSessionConfiguration:@"ud" ];
//backgorundSession is NSURLSession
self.backgroundSession =[NSURLSession sessionWithConfiguration:backgroundSessionConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:5000/upload/"]];
request1.HTTPMethod=@"POST";

uploadTask=[backgroundSession uploadTaskWithRequest:request fromFile:urlPath];
[uploadTask resume];

I am not able to suspend the task. Though it stops the execution of below method. But it still pushes the data to the server.

- URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: 

NSURLSession’s suspend and resume support is not really designed to implement a user-level ‘pause the transfer’ feature. Rather, it’s there to allow you to temporarily disable callbacks as part of some sort of concurrency control system. That's because, as you’ve noticed, a suspended task can still be active on the wire; all that the suspend does is prevent it from making progress internally, issuing callbacks, and so on.

If you’re implementing a user-level pause feature, my recommendation is as follows:

  • For downloads, you should call

    -cancelByProducingResumeData:
    and then resume with
    -downloadTaskWithResumeData:
    . This is supported by most servers.
  • For uploads, things get much trickier. Does your server explicitly support the ability to stop and resume the upload?

Share and Enjoy

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

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