以下代码用于使用post
向服务器发出AFHTTPClient
请求:
NSURL *url = [NSURL URLWithString:urlString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
//objects and keys
nil];
[httpClient postPath:postPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
id results = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONWritingPrettyPrinted error:nil];
//completion block
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
[httpClient release];
但是我注意到即使用户已从当前viewController
弹出(当记录超时错误时,注意到另一个viewController
时),请求仍在运行。
当viewController
不再位于视图层次结构中时,如何停止请求。
答案 0 :(得分:2)
你可以这样做:
[[httpClient operationQueue] cancelAllOperations];
或者这个:
[self cancelAllHTTPOperationsWithMethod:@"POST" path:@"/path"];
您可以将其中一个放入viewWillDisappear
;