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

时间:2017-05-10 20:15:20

标签: ios afnetworking afnetworking-3 afhttprequestoperation afhttpsessionmanager

我试图将曾经拥有AFNetworking 2.6.3的项目升级到AFNetworking 3.1。

我有一个网址请求:

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

以下是旧版本的代码:

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

我还没有触及过网址或网址请求。我试图将此呼叫转换为AFNetworking 3.1兼容呼叫。

这是我的新代码:

nil

但是,进程和成功/失败块都不会被调用。决不。我已经对所有内容进行了双重检查 - ~=,一切看起来都很完美。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我不得不打电话给[uploadOperation resume]开始操作。我最初认为resume只能在pause之后调用,而不是开始操作。致电resume解决了问题。