[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
This commit is contained in:
Mattt Thompson 2012-10-09 09:10:23 -07:00
parent b10f66062c
commit d3ae3d128f
2 changed files with 14 additions and 3 deletions

View file

@ -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. 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 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; - (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path;

View file

@ -525,13 +525,22 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {}
[self.operationQueue addOperation:operation]; [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]) { for (NSOperation *operation in [self.operationQueue operations]) {
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
continue; 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]; [operation cancel];
} }
} }