使用 AFHTTPSessionOperation 时超时
Posted
技术标签:
【中文标题】使用 AFHTTPSessionOperation 时超时【英文标题】:Timed Out while using AFHTTPSessionOperation 【发布时间】:2017-03-15 21:58:25 【问题描述】:谁能告诉我为什么我的通话“超时”?我的应用只是挂在那里,以下代码的success:^(NSURLSessionTask* operation, id response) 部分从未执行。
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;
【问题讨论】:
只有一个操作。我确实尝试将 maxConcurrentOperationCount 限制为 1,但它并没有解决我的问题。没有错误信息。我真的不知道从这里去哪里。 谢谢@Rob,它没有从第一次调用中击中成功块,没有失败,实际上它从第二次调用中执行了成功块,但在那之后,它只是无限期地冻结并且最终超时。令我困惑的是为什么在第二次调用成功完成后它没有返回到第一个成功块?它也没有从第一次调用中执行失败块。 【参考方案1】:您将块传递给beginRequestController
,但该方法对它们没有任何作用。你想调用这些块。例如
- (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;
或者,更简单:
- (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;
【讨论】:
你太棒了!这解决了它!以上是关于使用 AFHTTPSessionOperation 时超时的主要内容,如果未能解决你的问题,请参考以下文章
在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?