只需要在AFNetworking中执行一个HTTP操作,取消之前的一个

Need to execute only one HTTP operation in AFNetworking and cancel the previous one

我使用 AFNetworking 框架,需要对我的 Web 请求实施此逻辑:

我看到 AFHTTPRequestOperation 是一个 NSOperation 子类,所以我可以编写自己的请求管理器,但我看到框架中有 AFHTTPRequestOperationManager。我只发现我可以在这里做一个并发队列,这样我的请求就会一个接一个地执行。我可以使用 AFHTTPRequestOperationManager 获得所描述的行为吗?谢谢!

我也会把这个作为答案。所以想法是执行以下操作:

-为 AFNetworkingOperation 创建 属性:

@property AFHTTPRequestOperation *post;

-在您的初始化程序中:

self.post = nil;

-在调用请求的函数中:

if(self.post){
    [post cancel];
}

-您稍后需要分配self.post:

self.post = [manager POST:nil parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  self.post = nil;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   // error handling.
}];