AFNetworking取消successBlock

时间:2014-01-30 13:53:23

标签: ios objective-c afnetworking nsoperation

我想在再次运行请求时停止/取消操作。方法cancelAllHTTPOperationsWithMethod工作正常,但是当AFNetworking已经获取结果并且我的successBlock被解雇时我遇到了问题 - 我想在最近的将来停止它。但问题是operation.isCancelled没有被取消。

问题是我必须要做很长时间的成功吗?在NSOperation并取消它们或者是否有更简单快捷的方法?

代码:

[[AFHTTPClient sharedInstance] cancelAllHTTPOperationsWithMethod:@"GET" path:@"path"];
[[AFHTTPClient sharedInstance] getPath:@"path" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        for (longLoop) {
            // do something long 
            if (self.isCancelled) return; // this won't fire no matter how often i run it
        }
    });

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // do something to fail
}];

1 个答案:

答案 0 :(得分:0)

我最终在里面做了NSOperation。类似的东西:

[[AFHTTPClient sharedInstance] cancelAllHTTPOperationsWithMethod:@"GET" path:@"path"];
[operationQueue cancelAllOperations];

[[AFHTTPClient sharedInstance] getPath:@"path" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // add new operation
    MyOperation *successOperation = [[MyOperation alloc] init];
    [successOperation setResponseObject:responseObject];
    [operationQueue addOperation:successOperation];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // call delegate
    [self didFailFetchDataWithError:error];
}];