0

我有Singleton类 APIHTTPClient 负责使用AFHTTPRequestOperation**进行 Restful 调用。我有一个场景,用户单击按钮查看视图并调用 APIHTTPClient 以获取新数据以在该视图中显示它,但用户也可以单击后退按钮单击另一个视图并调用 APIHTTPClient 获取新视图的新数据。

当用户点击返回调用另一个restful Request时,如何取消之前的AFHTTPRequestOperation请求。

-(void)CallRemoteService:(int) iModuleID { static NSString * const BaseURLString = @" http://www.raywenderlich.com/demos/weather_sample/ "; NSString *string = [NSString stringWithFormat:@"%@weather.php?format=xml", BaseURLString];

NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


// Make sure to set the responseSerializer correctly

// operation.responseSerializer = [AFXMLParserResponseSerializer 序列化器];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    self.queue = [[NSOperationQueue alloc] init];
4

1 回答 1

0

在您的视图控制器中,保留对 AFHTTPRequestOperation *operation 的引用,然后按回:

[operation cancel]

于 2014-07-24T13:42:22.873 回答