使用iOS8中的NSUrlSession通过流请求进行后台上传 [英] Background Upload With Stream Request Using NSUrlSession in iOS8

查看:533
本文介绍了使用iOS8中的NSUrlSession通过流请求进行后台上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前在iOS7中,当我们尝试在后台使用流请求上传时,我们会遇到以下异常

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

由于未捕获的异常NSGenericException而终止应用,原因如下: 在后台会话中上传任务必须来自文件'

但是在iOS8中,当我们尝试在后台使用流上传时,没有例外。

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

现在我的问题是

1)在iOS8中允许使用 uploadTaskWithStreamedRequest:上传backgourd ?

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

2)在iOS8中,我使用背景NSURLConfiguration和 uploadTaskWithStreamedRequest 。我正在使用 - (void)URLSession:(NSURLSession *)会话任务:(NSURLSessionTask *)任务needNewBodyStream:(void(^)(NSInputStream *))completionHandler 为NSUrlSession提供流。当应用程序在前台时它工作正常并将我的文件上传到服务器。但是,只要应用程序在后台运行,流就会结束并且NSURLSession完成并出现以下错误

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

错误域= NSURLErrorDomain代码= -997与后台传输失去连接服务

我认为当应用程序进入后台时,我的流程结束。现在我的问题是,我应该在哪个runloop中安排我的Stream或让我知道我的理解是否有任何错误。

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);
}


推荐答案

您无法上传流使用后台配置的任务。我只在两种情况下成功上传数据:

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


  1. 使用存储在请求正文中的数据下载任务。

  2. 从文件上传任务。在这种情况下,您将不会收到回复正文。

这篇关于使用iOS8中的NSUrlSession通过流请求进行后台上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆