如何使用网络上传后台上传任务

编程入门 行业动态 更新时间:2024-10-27 08:27:04
本文介绍了如何使用网络上传后台上传任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用AFNetworking上传大文件,并在应用程序在后台时继续上传。

我可以上传文件,但是当例外:EXC_BAD_ACCESS(code = 1,address = 0x8000001f))

<$ p $ $ b $ _ $ _ $ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b _dispatch_root_queue_drain _dispatch_worker_thread3 _pthread_wqthread

以下是一些示例代码:注意:当我使用 [NSURLSessionConfiguration defaultSessionConfiguration] 时,上传成功,但当应用程序在后台时不会继续。 [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@company.appname.uploadservice] 导致应用程序崩溃。

NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@POSTURLString:[uploadBundle.uploadUrl absoluteString] parameters:[uploadBundle getParameters] constructBodyWithBlock:^(id< AFMultipartFormData> formData) $ b [formData appendPartWithFileURL:uploadBundle.fileDataUrl name:uploadBundle.fileName fileName:uploadBundle.fileName mimeType:uploadBundle.mimeType error:nil]; }错误:无]; 认证*认证= [认证getInstance]; [request addValue:authentication.token forHTTPHeaderField:@token]; [request addValue:authentication.authorization forHTTPHeaderField:@Authorization]; // NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@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 :%@,错误); } else { NSLog(@%@%@,response,responseObject); } }];

解决方案

我正在回答我自己的问题,希望它将帮助一些人。 $ b $我找不到任何地方记录,但它看起来像你必须使用 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:@filefilename:nil contentType:nil]; [multipartFormData addParameters:[uploadBundle getParameters]]; NSURLRequest * rq = [OMGHTTPURLRQ POST:[uploadBundle.uploadUrl absoluteString]:multipartFormData]; $ b $ id path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject] stringByAppendingPathComponent:@upload.NSData]; [rq.HTTPBody writeToFile:path atomically:YES]; [[session uploadTaskWithRequest:rq fromFile:[NSURL fileURLWithPath:path]] resume];

I'm trying to upload large files using AFNetworking and have the upload continue when the application is in the background.

I can upload files just fine, but when I attempt to use a background configuration -- the application crashes with the following stacktrace: Exception: EXC_BAD_ACCESS (code=1, address=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

Here is some example code:

Note: When I use [NSURLSessionConfiguration defaultSessionConfiguration] the upload succeeds but will not continue when the application is in the background. [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"company.appname.uploadservice"] causes the application to crash.

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:@"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); } }];

解决方案

I'm answering my own question in hopes that it will help a few people.

I can't find this documented anywhere, but it looks like you have to use uploadTaskWithRequest:fromFile:progress:completionHandler: when using a background session configuration.

Here is a simple example:

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];

更多推荐

如何使用网络上传后台上传任务

本文发布于:2023-11-26 05:47:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   如何使用   后台   网络

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!