【问题标题】:How to cancel AFHTTPRequestOperation?如何取消 AFHTTPRequestOperation?
【发布时间】:2014-07-24 13:36:36
【问题描述】:

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

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

-(void)CallRemoteService:(int) iModuleID { 静态 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];

【问题讨论】:

    标签: ios objective-c multithreading afhttprequestoperation


    【解决方案1】:

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

    [operation cancel]

    【讨论】:

    • 如果我想将 AFHTTPRequestOperation 代码与视图控制器分离并将所有 newtork 调用作为单个类放入,是否有任何替代解决方案?
    • 不管怎样,你需要告诉你的网络类从视图控制器取消操作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    相关资源
    最近更新 更多