3

我正在开发一个需要将大文件上传到服务器的项目。我使用 NSURLSession 来设置后台传输服务。一切似乎都很好,但是我相信它会继续发送 32K 的块。我想知道是否有办法增加块大小?

以下是设置上传任务的示例代码:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
sessionConfig.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [self configureURLRequest];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:filePath]];

[uploadTask resume];

这是委托示例代码:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {

DLog(@"didSendBodyData: %lld, totalBytesSent: %lld, totalBytesExpectedToSend: %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);    

}

同样,有没有办法增加使用 NSURLSession 上传的块大小?默认为 32K。

4

1 回答 1

0

使用 NSURLSession API 时无法控制块大小。作为旁注,您可能不想这样做。随着设备执行从 WiFi 更改为蜂窝网络或更换蜂窝塔等操作,您的带宽将会发生变化。我预计操作系统会不断适应这些复杂的变化变量,通过限制您的应用程序带宽来做出反应。你实际上想要这个,因为在一个脆弱的蜂窝网络上发出贪婪的请求很可能会失败。

于 2014-03-19T18:48:26.830 回答