我尝试使用AFNetworking上传大文件,并在应用程序处于后台时继续上传。
我可以上传文件,但是当我尝试使用后台配置时 - 应用程序崩溃了以下的堆栈跟踪: 例外:EXC_BAD_ACCESS(代码= 1,地址= 0x8000001f))
_CFStreamSetDispatchQueue
-[__NSCFBackgroundDataTask captureStream:]
__70-[__NSCFBackgroundDataTask _onqueue_needNewBodyStream:withCompletion:]_block_invoke_2
_dispatch_call_block_and_release
_dispatch_queue_drain
_dispatch_queue_invoke
_dispatch_root_queue_drain
_dispatch_worker_thread3
_pthread_wqthread
以下是一些示例代码:
注意:当我使用[NSURLSessionConfiguration defaultSessionConfiguration]
时,上传成功但在应用程序在后台时不会继续。 [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.company.appname.uploadservice"]
会导致应用程序崩溃。
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[uploadBundle.uploadUrl absoluteString] parameters:[uploadBundle getParameters] constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:uploadBundle.fileDataUrl name:uploadBundle.fileName fileName:uploadBundle.fileName mimeType:uploadBundle.mimeType error:nil];
} error:nil];
Authentication *authentication = [Authentication getInstance];
[request addValue:authentication.token forHTTPHeaderField:@"token"];
[request addValue:authentication.authorization forHTTPHeaderField:@"Authorization"];
//NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.company.appname.uploadservice"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSProgress *progress = nil;
_currentUploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
答案 0 :(得分:7)
我正在回答我自己的问题,希望能帮助一些人。
我无法在任何地方找到此文档,但在使用后台会话配置时,您似乎必须使用uploadTaskWithRequest:fromFile:progress:completionHandler:
。
这是一个简单的例子:
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
id config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.opentext.core.uploadservice"];
id session = [NSURLSession sessionWithConfiguration:config delegate:appDelegate delegateQueue:[NSOperationQueue new]];
OMGMultipartFormData *multipartFormData = [OMGMultipartFormData new];
//[multipartFormData addFile:data parameterName:@"file" filename:nil contentType:nil];
[multipartFormData addParameters:[uploadBundle getParameters]];
NSURLRequest *rq = [OMGHTTPURLRQ POST:[uploadBundle.uploadUrl absoluteString] :multipartFormData];
id path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"upload.NSData"];
[rq.HTTPBody writeToFile:path atomically:YES];
[[session uploadTaskWithRequest:rq fromFile:[NSURL fileURLWithPath:path]] resume];
答案 1 :(得分:0)
问题似乎是您正在尝试使用&#34;流式传输请求&#34;而不是文件。代码与
一起使用NSURLSessionUploadTask * uploadTask = [manager uploadTaskWithRequest:来自文件的请求:[NSURL fileURLWithPath:filePath]进度:&amp; progress completionHandler:^(NSURLResponse * response,id responseObject,NSError * error){
.... }];
没有任何问题的方法。我还发现,如果您尝试使用文件数据而不是实际文件(使用uploadTaskWithRequest:fromData:progress:completionHandler :),则上传失败,如Upload tasks from NSData are not supported in background sessions中所述