Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created April 11, 2014 16:03
Show Gist options
  • Select an option

  • Save joshdholtz/10480351 to your computer and use it in GitHub Desktop.

Select an option

Save joshdholtz/10480351 to your computer and use it in GitHub Desktop.
AFNetworking 2.0 - cancelAllHTTPOperationsWithMethod for AFHTTPRequestOperationManager
- (void)cancelAllHTTPOperationsWithMethod:(NSString*)method path:(NSString*)path parameters:(NSDictionary*)parameters ignoreParams:(BOOL)ignoreParams {
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:(method ?: @"GET") URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters];
NSString *URLStringToMatched = [[request URL] absoluteString];
for (NSOperation *operation in [self.operationQueue operations]) {
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
continue;
}
NSURL *matchymatchyURL = [[(AFHTTPRequestOperation *)operation request] URL];
if (ignoreParams == YES) {
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:matchymatchyURL resolvingAgainstBaseURL:YES];
components.query = nil;
components.fragment = nil;
matchymatchyURL = [components URL];
}
BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]];
BOOL hasMatchingURL = [[matchymatchyURL absoluteString] isEqualToString:URLStringToMatched];
if (hasMatchingMethod && hasMatchingURL) {
[operation cancel];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment