Timed Out while using AFHTTPSessionOperation

守給你的承諾、 提交于 2019-12-11 08:26:40

问题


Can anybody tell me why my call "timed out"? My app just hangs there, the success:^(NSURLSessionTask* operation, id response) section of the folowing code was never executed.

return [self beginRequestController:@"myController" action:@"myAction" parameters:parameters 
success:^(NSURLSessionTask* operation, id response)
{
    NSLog(@"This is NOT being called --->>>: %@",  response);
} failure:^(NSURLSessionTask* operation, NSError* error)
{
    //Handle the error
}];

- (NSOperation*) beginRequestController:(NSString*)controller action: (NSString*)action parameters:(NSDictionary*)parameters success: (RequestSuccess)success failure:(RequestFailure)failure
{
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress: nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Reponse --->>>: %@", responseObject );
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error --->>>: %@", error);
    }];
[self.operationQueue addOperation:operation];
return operation;
}

回答1:


You're passing blocks to beginRequestController, but that method doesn't do anything with them. You want to call those blocks. E.g.

- (NSOperation *)beginRequestController:(NSString *)controller action:(NSString *)action parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
    NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Response --->>>: %@", responseObject);
        if (success)
            success(task, responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error --->>>: %@", error);
        if (failure) 
            failure(task, error);
    }];
    [self.operationQueue addOperation:operation];
    return operation;
}

Or, even simpler:

- (NSOperation *)beginRequestController:(NSString *)controller action:(NSString *)action parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
    NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:@"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress: nil success:success failure:failure];
    [self.operationQueue addOperation:operation];
    return operation;
}


来源:https://stackoverflow.com/questions/42821463/timed-out-while-using-afhttpsessionoperation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!