@@ -60,12 +60,25 @@ - (void)threadEntryPoint
6060 }
6161}
6262
63+ - (void )performInThread : (NSThread *)thread usingBlock : (void (^)())block
64+ {
65+ [self performSelector: @selector (performUsingBlock: ) onThread: thread withObject: block waitUntilDone: NO ];
66+ }
67+
68+ - (void )performUsingBlock : (void (^)())block
69+ {
70+ block ();
71+ }
72+
6373- (void )addTask : (STNetTask *)task
6474{
6575 NSAssert (self.handler, @" STNetTaskQueueHandler is not set." );
6676 NSAssert (!task.finished, @" STNetTask is finished, please recreate a net task." );
77+
6778 task.pending = YES ;
68- [self performSelector: @selector (_addTask: ) onThread: self .thred withObject: task waitUntilDone: NO ];
79+ [self performInThread: self .thred usingBlock: ^{
80+ [self _addTask: task];
81+ }];
6982}
7083
7184- (void )_addTask : (STNetTask *)task
@@ -84,7 +97,10 @@ - (void)cancelTask:(STNetTask *)task
8497 if (!task) {
8598 return ;
8699 }
87- [self performSelector: @selector (_cancelTask: ) onThread: self .thred withObject: task waitUntilDone: NO ];
100+
101+ [self performInThread: self .thred usingBlock: ^{
102+ [self _cancelTask: task];
103+ }];
88104}
89105
90106- (void )_cancelTask : (STNetTask *)task
@@ -124,14 +140,13 @@ - (void)_sendWatingTasks
124140
125141- (void )task : (STNetTask *)task didResponse : (id )response
126142{
127- [self performSelector: @selector (_taskDidResponse: ) onThread: self .thred withObject: @{ @" task" : task, @" response" : response } waitUntilDone: NO ];
143+ [self performInThread: self .thred usingBlock: ^{
144+ [self _task: task didResponse: response];
145+ }];
128146}
129147
130- - (void )_taskDidResponse : ( NSDictionary *)params
148+ - (void )_task : (STNetTask *)task didResponse : ( id ) response
131149{
132- STNetTask *task = params[@" task" ];
133- id response = params[@" response" ];
134-
135150 if (![self .tasks containsObject: task]) {
136151 return ;
137152 }
@@ -144,7 +159,7 @@ - (void)_taskDidResponse:(NSDictionary *)params
144159 [STNetTaskQueueLog log: @" Exception in 'didResponse' - %@ " , exception.debugDescription];
145160 NSError *error = [NSError errorWithDomain: STNetTaskUnknownError
146161 code: -1
147- userInfo: @{ @" msg" : exception.description }];
162+ userInfo: @{ @" msg" : exception.description ? : @" nil " }];
148163
149164 if ([self _retryTask: task withError: error]) {
150165 return ;
@@ -164,14 +179,13 @@ - (void)_taskDidResponse:(NSDictionary *)params
164179
165180- (void )task : (STNetTask *)task didFailWithError : (NSError *)error
166181{
167- [self performSelector: @selector (_taskDidFailWithError: ) onThread: self .thred withObject: @{ @" task" : task, @" error" : error } waitUntilDone: NO ];
182+ [self performInThread: self .thred usingBlock: ^{
183+ [self _task: task didFailWithError: error];
184+ }];
168185}
169186
170- - (void )_taskDidFailWithError : ( NSDictionary *)params
187+ - (void )_task : (STNetTask *)task didFailWithError : ( NSError *) error
171188{
172- STNetTask *task = params[@" task" ];
173- NSError *error = params[@" error" ];
174-
175189 if (![self .tasks containsObject: task]) {
176190 return ;
177191 }
0 commit comments