From d3ae3d128ffc0bc32a391ffcc6eba9d6074f3298 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 9 Oct 2012 09:10:23 -0700 Subject: [PATCH] [Issue #465] Enforcing non-nil path constraint, and making it so that path is relative to the path of the client baseURL [Issue #465] Providing additional explanation about how to manage the lifecycle of enqueued operations --- AFNetworking/AFHTTPClient.h | 4 +++- AFNetworking/AFHTTPClient.m | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index 41354fb..e79f686 100755 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -300,7 +300,9 @@ typedef enum { Cancels all operations in the HTTP client's operation queue whose URLs match the specified HTTP method and path. @param method The HTTP method to match for the cancelled requests, such as `GET`, `POST`, `PUT`, or `DELETE`. If `nil`, all request operations with URLs matching the path will be cancelled. - @param path The path to match for the cancelled requests. + @param path The path appended to the HTTP client base URL to match against the cancelled requests. This parameter must not be `nil`. + + @discussion This method only cancels `AFHTTPRequestOperations` whose request URL matches the HTTP client base URL with the path appended. For complete control over the lifecycle of enqueued operations, you can access the `operationQueue` property directly, which allows you to, for instance, cancel operations filtered by a predicate, or simply use `-cancelAllRequests`. Note that the operation queue may include non-HTTP operations, so be sure to check the type before attempting to directly introspect an operation's `request` property. */ - (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path; diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 346bb76..5270dc1 100755 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -525,13 +525,22 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {} [self.operationQueue addOperation:operation]; } -- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path { +- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method + path:(NSString *)path +{ + NSCParameterAssert(path); + + NSString *URLStringToMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] absoluteString]; + for (NSOperation *operation in [self.operationQueue operations]) { if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { continue; } - if ((!method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]) && [path isEqualToString:[[[(AFHTTPRequestOperation *)operation request] URL] path]]) { + BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]; + BOOL hasMatchingURL = [[[[(AFHTTPRequestOperation *)operation request] URL] absoluteString] isEqualToString:URLStringToMatched]; + + if (hasMatchingMethod && hasMatchingURL) { [operation cancel]; } }