1

Previously in iOS7 when we try to upload with stream request in background we get following exception

Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks in background sessions must be from a file'

But in iOS8 there is no exception when we try to upload with stream in background.

Now my question is

1) Is backgourd upload with uploadTaskWithStreamedRequest: allowed in iOS8?

2) In iOS8 i am using background NSURLConfiguration with uploadTaskWithStreamedRequest. I am using -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *))completionHandler to provide stream to NSUrlSession. When app is in foreground it is working fine and uploading my file to the server. But as soon as the app igoes in background the stream ends and NSURLSession completes with following error

Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service"

I think when app goes in background my stream ends. Now my question is that in which runloop should I should I schedule my Stream or let me know if there is any mistake in my understanding.

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *))completionHandler
{
    // Open producer/consumer streams.  We open the producerStream straight
    // away.  We leave the consumerStream alone; NSURLConnection will deal
    // with it.
    NSLog(@"%@", [NSThread currentThread]);
    NSInputStream *consStream;
    NSOutputStream *prodStream;
    [NSStream createBoundInputStream:&consStream outputStream:&prodStream bufferSize:SFAMaxBufferLength];
    assert(consStream != nil);
    assert(prodStream != nil);
    self.consumerStream = consStream;
    self.producerStream = prodStream;
    self.producerStream.delegate = self;
    [self.producerStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [self.producerStream open];
    // Set up our state to send the body prefix first.
    self.buffer = [self.bodyPrefixData bytes];
    self.bufferLimit = [self.bodyPrefixData length];
    completionHandler(self.consumerStream);
}
Nauman Afzaal
  • 1,036
  • 12
  • 20

2 Answers2

2

You can't upload streamed tasks using Background Configuration. I successfully upload data only in two cases:

  1. Download task with data stored in request body.
  2. Upload task from file. In that case you will not receive response body.
Skie
  • 1,781
  • 14
  • 24
  • It would be great if you can share any document link to point that backgroundTask only supports upload from a file and Not from a body or NSStream ? – Ekra Apr 07 '15 at 14:39
  • 1
    Unfortunately they didn't document a lot of things regarding Background Transfer. All that I know because we done a lot of research. – Skie Apr 10 '15 at 09:30
0

You can upload a multipart file in background - just that this is not straight forward. Refer: AFNetworking error in uploadTaskWithStreamedRequest

Community
  • 1
  • 1
tuttu47
  • 481
  • 1
  • 4
  • 19
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference – kleopatra Aug 18 '15 at 06:01