AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest 处理程序未调用
Posted
技术标签:
【中文标题】AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest 处理程序未调用【英文标题】:AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest handlers not called 【发布时间】:2017-05-10 20:15:19 【问题描述】:我正在尝试将曾经拥有 AFNetworking 2.6.3 的项目升级到 AFNetworking 3.1。
我有一个 URL 请求:
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"PUT"];
[request setValue:mimeType forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
[request setURL:requestURL];
这是旧版本的代码:
__block AFHTTPRequestOperation *uploadOperation;
uploadOperation = [[AFHTTPRequestOperationManager manager] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
//something here (gets called if successful)
failure:^(AFHTTPRequestOperation *operation, NSError *error)
//something here (gets called if failed)
] ;
[uploadOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
//this gets repeatedly called where I could calculate percentage
];
[uploadOperations addObject:uploadOperation];
我没有触及 URL 或 URL 请求。我正在尝试将此调用转换为兼容 AFNetworking 3.1 的调用。
这是我的新代码:
__block NSURLSessionUploadTask *uploadOperation;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//manager.responseSerializer = [AFJSONResponseSerializer serializer];
uploadOperation = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress)
//this should be called on upload
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
if(error)
//this should be called in case of failure
else
//this should be called if file gets uploaded successfully.
];
[uploadOperations addObject:uploadOperation];
但是,进度块和成功/失败块都不会被调用。绝不。我已经仔细检查了所有内容都不是nil
,否则一切看起来都很完美。
我做错了什么?
【问题讨论】:
【参考方案1】:我必须致电[uploadOperation resume]
才能开始操作。我最初认为resume
只是在pause
之后调用,而不是开始操作。致电resume
解决了这个问题。
【讨论】:
以上是关于AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest 处理程序未调用的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 存储等待 uploadTask.onComplete 已过时
Flutter -> UploadTask 方法 OnComplete 不存在
iOS:didCompleteWithError、didReceive 响应未由 URLSession.uploadTask 触发
uploadTask 成功后,ref.downloadURL() 不执行或返回 url