Skip to content

Commit c216bc4

Browse files
committed
Use block in STNetTaskQueue for switching thread.
Use map table for mapping net task to session task.
1 parent d967654 commit c216bc4

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

STNetTaskQueue/STHTTPNetTask.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
NSString *const STHTTPNetTaskErrorResponseDataUserInfoKey = @"responseData";
1616
NSString *STHTTPNetTaskRequestObjectDefaultSeparator = @"_";
1717

18-
@interface STHTTPNetTask ()
19-
20-
@property (nonatomic, strong) NSURLSessionTask *sessionTask;
21-
22-
@end
23-
2418
@implementation STHTTPNetTask
2519

2620
- (STHTTPNetTaskMethod)method

STNetTaskQueue/STHTTPNetTaskQueueHandler.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ - (STHTTPNetTaskQueueHandlerOperation *)operation
7878
static NSDictionary *STHTTPNetTaskMethodMap;
7979
static NSDictionary *STHTTPNetTaskContentTypeMap;
8080
static NSString *STHTTPNetTaskFormDataBoundary;
81+
static NSMapTable *STHTTPNetTaskToSessionTask;
8182

8283
@interface STHTTPNetTaskQueueHandlerOperation : NSObject <NSURLSessionDataDelegate>
8384

8485
@property (nonatomic, strong) STNetTaskQueue *queue;
85-
@property (nonatomic, weak) STHTTPNetTask *task;
86+
@property (nonatomic, strong) STHTTPNetTask *task;
8687
@property (nonatomic, strong) NSURLSession *session;
8788
@property (nonatomic, strong) NSURL *baseURL;
8889

@@ -106,6 +107,7 @@ + (void)load
106107
STHTTPNetTaskContentTypeMap = @{ @(STHTTPNetTaskRequestJSON): @"application/json; charset=utf-8",
107108
@(STHTTPNetTaskRequestKeyValueString): @"application/x-www-form-urlencoded" };
108109
STHTTPNetTaskFormDataBoundary = [NSString stringWithFormat:@"ST-Boundary-%@", [[NSUUID UUID] UUIDString]];
110+
STHTTPNetTaskToSessionTask = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsWeakMemory capacity:50];
109111
}
110112

111113
- (void)start
@@ -164,7 +166,7 @@ - (void)start
164166
break;
165167
}
166168

167-
[_task setValue:sessionTask forKey:@"sessionTask"];
169+
[STHTTPNetTaskToSessionTask setObject:sessionTask forKey:_task];
168170

169171
sessionTask.operation = self;
170172
[sessionTask resume];
@@ -396,12 +398,8 @@ - (void)netTaskQueue:(STNetTaskQueue *)netTaskQueue didCancelTask:(STNetTask *)t
396398
{
397399
NSAssert([task isKindOfClass:[STHTTPNetTask class]], @"Net task should be subclass of STHTTPNetTask");
398400

399-
STHTTPNetTask *httpTask = (STHTTPNetTask *)task;
400-
401-
NSURLSessionTask *sessionTask = [httpTask valueForKey:@"sessionTask"];
401+
NSURLSessionTask *sessionTask = [STHTTPNetTaskToSessionTask objectForKey:task];
402402
[sessionTask cancel];
403-
404-
[httpTask setValue:nil forKey:@"sessionTask"];
405403
}
406404

407405
- (void)netTaskQueueDidBecomeInactive:(STNetTaskQueue *)netTaskQueue

STNetTaskQueue/STNetTaskQueue.m

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)