停止当前在popViewController上运行HTTP Post操作

时间:2013-09-11 10:05:35

标签: ios

以下代码用于使用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不再位于视图层次结构中时,如何停止请求。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

[[httpClient operationQueue] cancelAllOperations];

或者这个:

[self cancelAllHTTPOperationsWithMethod:@"POST" path:@"/path"];

您可以将其中一个放入viewWillDisappear;