├── AFHTTPClient+ProxyQueue.h ├── AFHTTPClient+ProxyQueue.m └── README.md /AFHTTPClient+ProxyQueue.h: -------------------------------------------------------------------------------- 1 | // AFHTTPClient+ProxyQueue.h 2 | // 3 | // Copyright (c) 2012 James Tang (mystcolor@gmail.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFHTTPClient.h" 24 | 25 | @interface AFHTTPClient (ProxyQueue) 26 | 27 | - (AFHTTPClient *)proxyQueueNamed:(NSString *)name; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /AFHTTPClient+ProxyQueue.m: -------------------------------------------------------------------------------- 1 | // AFHTTPClient+ProxyQueue.m 2 | // 3 | // Copyright (c) 2012 James Tang (mystcolor@gmail.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFHTTPClient+ProxyQueue.h" 24 | #import "AFNetworking.h" 25 | #import 26 | 27 | typedef void (^AFCompletionBlock)(void); 28 | 29 | @interface AFHTTPClientProxy : NSProxy 30 | 31 | @property (nonatomic, strong) NSOperationQueue *operationQueue; 32 | @property (nonatomic, weak) id target; 33 | 34 | + (AFHTTPClientProxy *)proxyWithTarget:(id)target; 35 | 36 | @end 37 | 38 | 39 | @implementation AFHTTPClientProxy 40 | 41 | - (id)init { 42 | _operationQueue = [[NSOperationQueue alloc] init]; 43 | _operationQueue.maxConcurrentOperationCount = 1; 44 | return self; 45 | } 46 | 47 | - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { 48 | return [self.target methodSignatureForSelector:sel]; 49 | } 50 | 51 | - (void)forwardInvocation:(NSInvocation *)invocation { 52 | [invocation invokeWithTarget:self.target]; 53 | } 54 | 55 | - (void)dealloc { 56 | [self.operationQueue cancelAllOperations]; 57 | } 58 | 59 | + (AFHTTPClientProxy *)proxyWithTarget:(id)target { 60 | AFHTTPClientProxy *proxy = [[AFHTTPClientProxy alloc] init]; 61 | proxy.target = target; 62 | return proxy; 63 | } 64 | 65 | #pragma mark - AFHTTPClient overrides 66 | 67 | - (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation { 68 | [self.operationQueue addOperation:operation]; 69 | } 70 | 71 | - (void)cancelAllHTTPOperationsWithMethod:(NSString *)method 72 | path:(NSString *)path 73 | { 74 | NSString *URLStringToMatched = [[[self.target requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] absoluteString]; 75 | 76 | for (NSOperation *operation in [self.operationQueue operations]) { 77 | if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { 78 | continue; 79 | } 80 | 81 | BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]; 82 | BOOL hasMatchingURL = [[[[(AFHTTPRequestOperation *)operation request] URL] absoluteString] isEqualToString:URLStringToMatched]; 83 | 84 | if (hasMatchingMethod && hasMatchingURL) { 85 | [operation cancel]; 86 | } 87 | } 88 | } 89 | 90 | - (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 91 | progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock 92 | completionBlock:(void (^)(NSArray *operations))completionBlock 93 | { 94 | __block dispatch_group_t dispatchGroup = dispatch_group_create(); 95 | NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ 96 | dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{ 97 | if (completionBlock) { 98 | completionBlock(operations); 99 | } 100 | }); 101 | #if !OS_OBJECT_USE_OBJC 102 | dispatch_release(dispatchGroup); 103 | #endif 104 | }]; 105 | 106 | for (AFHTTPRequestOperation *operation in operations) { 107 | AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy]; 108 | operation.completionBlock = ^{ 109 | dispatch_queue_t queue = operation.successCallbackQueue ?: dispatch_get_main_queue(); 110 | dispatch_group_async(dispatchGroup, queue, ^{ 111 | if (originalCompletionBlock) { 112 | originalCompletionBlock(); 113 | } 114 | 115 | __block NSUInteger numberOfFinishedOperations = 0; 116 | [operations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 117 | if ([(NSOperation *)obj isFinished]) { 118 | numberOfFinishedOperations++; 119 | } 120 | }]; 121 | 122 | if (progressBlock) { 123 | progressBlock(numberOfFinishedOperations, [operations count]); 124 | } 125 | 126 | dispatch_group_leave(dispatchGroup); 127 | }); 128 | }; 129 | 130 | dispatch_group_enter(dispatchGroup); 131 | [batchedOperation addDependency:operation]; 132 | } 133 | [self.operationQueue addOperations:operations waitUntilFinished:NO]; 134 | [self.operationQueue addOperation:batchedOperation]; 135 | } 136 | 137 | @end 138 | 139 | 140 | @interface AFHTTPClient (ProxyQueuePrivate) 141 | 142 | @property (nonatomic, strong) NSMutableDictionary *proxiesDict; 143 | 144 | @end 145 | 146 | 147 | @implementation AFHTTPClient (ProxyQueue) 148 | 149 | static char *proxiesDictKey; 150 | 151 | - (void)setProxiesDict:(NSMutableDictionary *)proxiesDict { 152 | objc_setAssociatedObject(self, &proxiesDictKey, proxiesDict, OBJC_ASSOCIATION_RETAIN); 153 | } 154 | 155 | - (NSMutableDictionary *)proxiesDict { 156 | return objc_getAssociatedObject(self, &proxiesDictKey); 157 | } 158 | 159 | - (AFHTTPClient *)proxyQueueNamed:(NSString *)name { 160 | if ( ! self.proxiesDict) { 161 | self.proxiesDict = [NSMutableDictionary dictionary]; 162 | } 163 | 164 | AFHTTPClientProxy *proxy = [self.proxiesDict objectForKey:name]; 165 | if ( ! proxy) { 166 | proxy = [AFHTTPClientProxy proxyWithTarget:self]; 167 | [self.proxiesDict setObject:proxy forKey:name]; 168 | } 169 | 170 | return (AFHTTPClient *)proxy; 171 | } 172 | 173 | @end 174 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AFNetworking-ProxyQueue 2 | ======================= 3 | 4 | AFNetworking with multiple operation queue support, to separate download operations from affecting too much on main network request. 5 | 6 | 7 | ```objective-c 8 | 9 | // Original 10 | [[YourHTTPClient sharedClient] enqueueHTTPRequestOperation:usualNetworkOperation]; 11 | 12 | // Dispatch to another shared queue for download operation 13 | [[YourHTTPClient sharedClient] proxyQueueNamed:@"downloadQueue"] enqueueHTTPRequestOperation:downloadOperation]; 14 | 15 | ``` 16 | --------------------------------------------------------------------------------