Compare commits

...

2 Commits
master ... 2974

Author SHA1 Message Date
Kevin Harwood
d284c55180 Refactored task description usage and added some tests 2015-10-15 14:55:40 -05:00
Phil Tang
492eead997 Let app devs use taskDescription by using associated objects internally 2015-10-15 13:36:59 -05:00
2 changed files with 85 additions and 22 deletions

View File

@ -286,6 +286,7 @@ static inline BOOL af_addMethod(Class class, SEL selector, Method method) {
return class_addMethod(class, selector, method_getImplementation(method), method_getTypeEncoding(method));
}
static void *AFNSURLSessionTaskSessionManagerKey = &AFNSURLSessionTaskSessionManagerKey;
static NSString * const AFNSURLSessionTaskDidResumeNotification = @"com.alamofire.networking.nsurlsessiontask.resume";
static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofire.networking.nsurlsessiontask.suspend";
@ -396,7 +397,6 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
@property (readwrite, nonatomic, strong) NSOperationQueue *operationQueue;
@property (readwrite, nonatomic, strong) NSURLSession *session;
@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableTaskDelegatesKeyedByTaskIdentifier;
@property (readonly, nonatomic, copy) NSString *taskDescriptionForSessionTasks;
@property (readwrite, nonatomic, strong) NSLock *lock;
@property (readwrite, nonatomic, copy) AFURLSessionDidBecomeInvalidBlock sessionDidBecomeInvalid;
@property (readwrite, nonatomic, copy) AFURLSessionDidReceiveAuthenticationChallengeBlock sessionDidReceiveAuthenticationChallenge;
@ -477,29 +477,23 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
#pragma mark -
- (NSString *)taskDescriptionForSessionTasks {
return [NSString stringWithFormat:@"%p", self];
}
- (void)taskDidResume:(NSNotification *)notification {
NSURLSessionTask *task = notification.object;
if ([task respondsToSelector:@selector(taskDescription)]) {
if ([task.taskDescription isEqualToString:self.taskDescriptionForSessionTasks]) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidResumeNotification object:task];
});
}
AFURLSessionManager *manager = [self managerForTask:task];
if (self == manager) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidResumeNotification object:task];
});
}
}
- (void)taskDidSuspend:(NSNotification *)notification {
NSURLSessionTask *task = notification.object;
if ([task respondsToSelector:@selector(taskDescription)]) {
if ([task.taskDescription isEqualToString:self.taskDescriptionForSessionTasks]) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidSuspendNotification object:task];
});
}
AFURLSessionManager *manager = [self managerForTask:task];
if (self == manager) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidSuspendNotification object:task];
});
}
}
@ -534,7 +528,7 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
delegate.manager = self;
delegate.completionHandler = completionHandler;
dataTask.taskDescription = self.taskDescriptionForSessionTasks;
[self setManagerForTask:dataTask];
[self setDelegate:delegate forTask:dataTask];
}
@ -571,8 +565,7 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
*progress = delegate.progress;
}
uploadTask.taskDescription = self.taskDescriptionForSessionTasks;
[self setManagerForTask:uploadTask];
[self setDelegate:delegate forTask:uploadTask];
}
@ -595,8 +588,7 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
*progress = delegate.progress;
}
downloadTask.taskDescription = self.taskDescriptionForSessionTasks;
[self setManagerForTask:downloadTask];
[self setDelegate:delegate forTask:downloadTask];
}
@ -854,6 +846,15 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
self.downloadTaskDidResume = block;
}
#pragma mark -
- (void)setManagerForTask:(NSURLSessionTask *)task {
objc_setAssociatedObject(task, AFNSURLSessionTaskSessionManagerKey, self, OBJC_ASSOCIATION_ASSIGN);
}
- (nullable instancetype)managerForTask:(NSURLSessionTask *)task {
return objc_getAssociatedObject(task, AFNSURLSessionTaskSessionManagerKey);
}
#pragma mark - NSObject
- (NSString *)description {

View File

@ -161,6 +161,68 @@
XCTAssertNotNil(downloadFilePath);
}
#pragma mark -
- (void)testTaskDescriptionIsNilByDefault {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should finish"];
NSURLSessionTask *testTask;
testTask = [self.manager
GET:request.URL.absoluteString
parameters:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
XCTAssertNil(task.taskDescription, @"taskDescription should be nil when request ends.");
[expectation fulfill];
}
failure:nil];
XCTAssertNil(testTask.taskDescription, @"taskDescription should be nil when request starts.");
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testThatTaskWithCustomTaskDescriptionPostsResumeNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should finish"];
NSString *taskDescription = @"this is a test!";
NSURLSessionTask *testTask;
testTask = [self.manager
dataTaskWithRequest:request
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
[expectation fulfill];
}];
testTask.taskDescription = taskDescription;
[testTask resume];
[self expectationForNotification:AFNetworkingTaskDidResumeNotification
object:nil
handler:^BOOL(NSNotification * _Nonnull notification) {
XCTAssertTrue([[(NSURLSessionTask *)notification.object taskDescription] isEqualToString:taskDescription]);
return YES;
}];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
- (void)testThatTaskWithCustomTaskDescriptionPostsSuspendNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should finish"];
NSString *taskDescription = @"this is a test!";
NSURLSessionTask *testTask;
testTask = [self.manager
dataTaskWithRequest:request
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
[expectation fulfill];
}];
testTask.taskDescription = taskDescription;
[testTask resume];
[testTask suspend];
[self expectationForNotification:AFNetworkingTaskDidSuspendNotification
object:nil
handler:^BOOL(NSNotification * _Nonnull notification) {
XCTAssertTrue([[(NSURLSessionTask *)notification.object taskDescription] isEqualToString:taskDescription]);
return YES;
}];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
@end