GeezerVison is showing github.com — Added -subscribeState:usingBlock: · kevin-lyn/STNetTaskQueue@595b204
File treeExpand file treeCollapse file tree Open diff view settings STNetTaskQueueTest/STNetTaskQueueTest
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: STNetTaskQueue/STNetTask.h +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,7 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError; | 23 | 23 | }]; \ | 24 | 24 | return nil; \ | 25 | 25 | }] | | 26 | + | 26 | 27 | #endif | 27 | 28 |
| 28 | 29 | @class STNetTask; | @@ -39,6 +40,14 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError; | 39 | 40 |
| 40 | 41 | @end | 41 | 42 |
| | 43 | +typedef NS_ENUM(NSUInteger, STNetTaskState) { | | 44 | + STNetTaskStateCancalled, | | 45 | + STNetTaskStateFinished, | | 46 | + STNetTaskStateRetrying | | 47 | +}; | | 48 | + | | 49 | +typedef void (^STNetTaskSubscriptionBlock)(STNetTask *task); | | 50 | + | 42 | 51 | @interface STNetTask : NSObject | 43 | 52 |
| 44 | 53 | /* Error object which contains error message when net task is failed. */ | @@ -71,18 +80,21 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError; | 71 | 80 |
| 72 | 81 | /* | 73 | 82 | A callback method which is called when the net task is finished successfully. | | 83 | + Note: this method will be called in thread of STNetTaskQueue. | 74 | 84 | | 75 | 85 | @param response id The response object. | 76 | 86 | */ | 77 | 87 | - (void)didResponse:(id)response; | 78 | 88 |
| 79 | 89 | /* | 80 | 90 | A callback method which is called when the net task is failed. | | 91 | + Note: this method will be called in thread of STNetTaskQueue. | 81 | 92 | */ | 82 | 93 | - (void)didFail; | 83 | 94 |
| 84 | 95 | /* | 85 | 96 | A callback method which is called when the net task is retried. | | 97 | + Note: this method will be called in thread of STNetTaskQueue. | 86 | 98 | */ | 87 | 99 | - (void)didRetry; | 88 | 100 |
| @@ -110,4 +122,13 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError; | 110 | 122 | */ | 111 | 123 | - (NSTimeInterval)retryInterval; | 112 | 124 |
| | 125 | +/* | | 126 | + Subscribe state of net task by using block | | 127 | + | | 128 | + @param state STNetTaskState state of net task | | 129 | + @param block STNetTaskSubscriptionBlock block is called when net task is in subscribed state. | | 130 | + NOTE: this block will be called in main thread. | | 131 | + */ | | 132 | +- (void)subscribeState:(STNetTaskState)state usingBlock:(STNetTaskSubscriptionBlock)block; | | 133 | + | 113 | 134 | @end |
Collapse file Copy file name to clipboardExpand all lines: STNetTaskQueue/STNetTask.m +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ @interface STNetTask () | 16 | 16 | @property (atomic, assign) BOOL cancelled; | 17 | 17 | @property (atomic, assign) BOOL finished; | 18 | 18 | @property (atomic, assign) NSUInteger retryCount; | | 19 | +@property (nonatomic, strong) NSMutableDictionary *stateToBlock; | 19 | 20 |
| 20 | 21 | @end | 21 | 22 |
| @@ -56,4 +57,33 @@ - (NSTimeInterval)retryInterval | 56 | 57 | return 0; | 57 | 58 | } | 58 | 59 |
| | 60 | +- (void)subscribeState:(STNetTaskState)state usingBlock:(STNetTaskSubscriptionBlock)block | | 61 | +{ | | 62 | + if (!self.stateToBlock) { | | 63 | + self.stateToBlock = [NSMutableDictionary new]; | | 64 | + } | | 65 | + NSAssert(self.stateToBlock[@(state)] == nil, @"State is subscribed already"); | | 66 | + self.stateToBlock[@(state)] = [block copy]; | | 67 | +} | | 68 | + | | 69 | +- (void)notifyState:(STNetTaskState)state | | 70 | +{ | | 71 | + dispatch_async(dispatch_get_main_queue(), ^{ | | 72 | + STNetTaskSubscriptionBlock block = self.stateToBlock[@(state)]; | | 73 | + if (block) { | | 74 | + block(self); | | 75 | + } | | 76 | + | | 77 | + switch (state) { | | 78 | + case STNetTaskStateFinished: | | 79 | + case STNetTaskStateCancalled: { | | 80 | + self.stateToBlock = nil; | | 81 | + } | | 82 | + break; | | 83 | + default: | | 84 | + break; | | 85 | + } | | 86 | + }); | | 87 | +} | | 88 | + | 59 | 89 | @end |
Collapse file Copy file name to clipboardExpand all lines: STNetTaskQueue/STNetTaskQueue.m +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,8 @@ @interface STNetTask (STInternal) | 16 | 16 | @property (atomic, assign) BOOL finished; | 17 | 17 | @property (atomic, assign) NSUInteger retryCount; | 18 | 18 |
| | 19 | +- (void)notifyState:(STNetTaskState)state; | | 20 | + | 19 | 21 | @end | 20 | 22 |
| 21 | 23 | @interface STNetTaskQueue() | @@ -120,6 +122,7 @@ - (void)_cancelTask:(STNetTask *)task | 120 | 122 |
| 121 | 123 | [self.handler netTaskQueue:self didCancelTask:task]; | 122 | 124 | task.cancelled = YES; | | 125 | + [task notifyState:STNetTaskStateCancalled]; | 123 | 126 | } | 124 | 127 |
| 125 | 128 | - (BOOL)_retryTask:(STNetTask *)task withError:(NSError *)error | @@ -136,6 +139,7 @@ - (void)_retryTask:(STNetTask *)task | 136 | 139 | { | 137 | 140 | if (!task.cancelled) { | 138 | 141 | [task didRetry]; | | 142 | + [task notifyState:STNetTaskStateRetrying]; | 139 | 143 | [self addTask:task]; | 140 | 144 | } | 141 | 145 | } | @@ -183,6 +187,7 @@ - (void)_task:(STNetTask *)task didResponse:(id)response | 183 | 187 |
| 184 | 188 | task.pending = NO; | 185 | 189 | task.finished = YES; | | 190 | + [task notifyState:STNetTaskStateFinished]; | 186 | 191 |
| 187 | 192 | [self _netTaskDidEnd:task]; | 188 | 193 |
| @@ -213,6 +218,7 @@ - (void)_task:(STNetTask *)task didFailWithError:(NSError *)error | 213 | 218 | [task didFail]; | 214 | 219 | task.pending = NO; | 215 | 220 | task.finished = YES; | | 221 | + [task notifyState:STNetTaskStateFinished]; | 216 | 222 |
| 217 | 223 | [self _netTaskDidEnd:task]; | 218 | 224 |
| |
Collapse file Copy file name to clipboardExpand all lines: STNetTaskQueueTest/STNetTaskQueueTest/STNetTaskQueueTestHTTP.m +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -185,6 +185,28 @@ - (void)testTaskDelegateForClass | 185 | 185 | [self waitForExpectationsWithTimeout:10 handler:nil]; | 186 | 186 | } | 187 | 187 |
| | 188 | +- (void)testSubscriptionBlock | | 189 | +{ | | 190 | + [self setUpNetTaskQueueWithBaseURLString:@"http://jsonplaceholder.typicode.com"]; | | 191 | + | | 192 | + _expectation = [self expectationWithDescription:@"testSubscriptionBlock"]; | | 193 | + | | 194 | + STTestGetNetTask *testGetTask = [STTestGetNetTask new]; | | 195 | + testGetTask.id = 1; | | 196 | + [[STNetTaskQueue sharedQueue] addTask:testGetTask]; | | 197 | + | | 198 | + [testGetTask subscribeState:STNetTaskStateFinished usingBlock:^(STNetTask *task) { | | 199 | + if (task.finished) { | | 200 | + [_expectation fulfill]; | | 201 | + } | | 202 | + else { | | 203 | + XCTFail(@"%@ failed", _expectation.description); | | 204 | + } | | 205 | + }]; | | 206 | + | | 207 | + [self waitForExpectationsWithTimeout:10 handler:nil]; | | 208 | +} | | 209 | + | 188 | 210 | - (void)netTaskDidEnd:(STNetTask *)task | 189 | 211 | { | 190 | 212 | if (!_expectation) { | |
|