我想在再次运行请求时停止/取消操作。方法cancelAllHTTPOperationsWithMethod
的工作正常,但是当afnetworking已经取得了结果并且我的成功板被触发时,我有一个问题 - 我想在不久的将来停止它。但是问题是该操作未取消。
问题是我必须在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
}];
我在内部执行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];
}];