iosafnetworkingafnetworking-3afhttprequestoperationafhttpsessionmanager

AFNetworking 3 uploadTaskWithRequest / uploadTaskWithStreamedRequest handlers not called


I'm trying to upgrade a project that used to have AFNetworking 2.6.3 to AFNetworking 3.1.

I have a URL request:

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

Here is a code from old version:

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

I haven't touched the URL or the URL request. I'm trying to convert this call to AFNetworking 3.1-compatible call.

Here is my new code:

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

However, neither the progress nor the success/failure block gets called. Never. I've double-checked everything to be non-nil, and everything seems perfect otherwise.

What am I doing wrong?


Solution

  • I had to call [uploadOperation resume] to start the operation. I've initially thought resume was only to be called after pause, not to begin the operation. Calling resume fixed the issue.