Skip to content

Commit 917d709

Browse files
committed
Added "cancelled" property in net task.
Send "error" when net task is cancelled when using RAC.
1 parent c7afa4a commit 917d709

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

STNetTaskQueue/STNetTask.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError;
1818
[subscriber sendNext:TASK];\
1919
[subscriber sendCompleted]; \
2020
}]; \
21+
[[[[RACObserve(TASK, cancelled) skip:1] ignore:@NO] deliverOnMainThread] subscribeNext:^(id x) { \
22+
[subscriber sendError:nil];\
23+
}]; \
2124
return nil; \
2225
}]
2326
#endif
@@ -47,6 +50,12 @@ FOUNDATION_EXPORT NSString *const STNetTaskUnknownError;
4750
*/
4851
@property (atomic, assign) BOOL pending;
4952

53+
/*
54+
Indicates if the net task is cancelled.
55+
This value would be "NO" by default after net task is created, even the net task is not added to queue.
56+
*/
57+
@property (atomic, assign) BOOL cancelled;
58+
5059
/* Indicates if the net task is finished(no matter it's successful or failed). */
5160
@property (atomic, assign) BOOL finished;
5261

STNetTaskQueue/STNetTaskQueue.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ - (void)performUsingBlock:(void(^)())block
7373
- (void)addTask:(STNetTask *)task
7474
{
7575
NSAssert(self.handler, @"STNetTaskQueueHandler is not set.");
76-
NSAssert(!task.finished, @"STNetTask is finished, please recreate a net task.");
76+
NSAssert(!task.finished && !task.cancelled, @"STNetTask is finished/cancelled, please recreate a net task.");
7777

7878
task.pending = YES;
7979
[self performInThread:self.thred usingBlock:^{
@@ -110,6 +110,7 @@ - (void)_cancelTask:(STNetTask *)task
110110
task.pending = NO;
111111

112112
[self.handler netTaskQueue:self didCancelTask:task];
113+
task.cancelled = YES;
113114
}
114115

115116
- (BOOL)_retryTask:(STNetTask *)task withError:(NSError *)error

0 commit comments

Comments
 (0)