gpt4 book ai didi

ios - AFNetworking 3 uploadTaskWithRequest/uploadTaskWithStreamedRequest 处理程序未调用

转载 作者:行者123 更新时间:2023-11-28 21:10:03 26 4
gpt4 key购买 nike

我正在尝试将以前使用 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];

但是,进度 block 和成功/失败 block 都不会被调用。绝不。我已经仔细检查过所有内容都不是nil,否则一切似乎都很完美。

我做错了什么?

最佳答案

我必须调用 [uploadOperation resume] 才能开始操作。我最初认为resume只是在pause之后被调用,而不是开始操作。调用 resume 解决了这个问题。

关于ios - AFNetworking 3 uploadTaskWithRequest/uploadTaskWithStreamedRequest 处理程序未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43901953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com