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

AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest handlers not called

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

但是,进度和 success/failure 块都不会被调用。绝不。我已经仔细检查了一切都不是 nil,否则一切似乎都很完美。

我做错了什么?

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