Skip to content

Commit

Permalink
Delete unused af_url_session_manager_creation_queue when don’t sup…
Browse files Browse the repository at this point in the history
…port below iOS 8 (AFNetworking#4401)

* Delete unused queue when iOS deployment had changed

* Remove unrelated test code when iOS8+
  • Loading branch information
kinarobin authored Mar 27, 2020
1 parent 3f2e551 commit 67f558f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 89 deletions.
75 changes: 14 additions & 61 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,6 @@
#import "AFURLSessionManager.h"
#import <objc/runtime.h>

#ifndef NSFoundationVersionNumber_iOS_8_0
#define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug 1140.11
#else
#define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug NSFoundationVersionNumber_iOS_8_0
#endif

static dispatch_queue_t url_session_manager_creation_queue() {
static dispatch_queue_t af_url_session_manager_creation_queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
af_url_session_manager_creation_queue = dispatch_queue_create("com.alamofire.networking.session.manager.creation", DISPATCH_QUEUE_SERIAL);
});

return af_url_session_manager_creation_queue;
}

static void url_session_manager_create_task_safely(dispatch_block_t _Nonnull block) {
if (block != NULL) {
if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) {
// Fix of bug
// Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8)
// Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093
dispatch_sync(url_session_manager_creation_queue(), block);
} else {
block();
}
}
}

static dispatch_queue_t url_session_manager_processing_queue() {
static dispatch_queue_t af_url_session_manager_processing_queue;
static dispatch_once_t onceToken;
Expand Down Expand Up @@ -769,10 +740,7 @@ - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler {

__block NSURLSessionDataTask *dataTask = nil;
url_session_manager_create_task_safely(^{
dataTask = [self.session dataTaskWithRequest:request];
});
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:request];

[self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];

Expand All @@ -786,17 +754,14 @@ - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
__block NSURLSessionUploadTask *uploadTask = nil;
url_session_manager_create_task_safely(^{
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];

// uploadTask may be nil on iOS7 because uploadTaskWithRequest:fromFile: may return nil despite being documented as nonnull (https://devforums.apple.com/message/926113#926113)
if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
}
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];

// uploadTask may be nil on iOS7 because uploadTaskWithRequest:fromFile: may return nil despite being documented as nonnull (https://devforums.apple.com/message/926113#926113)
if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
}
});
}

if (uploadTask) {
[self addDelegateForUploadTask:uploadTask
Expand All @@ -812,11 +777,8 @@ - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
__block NSURLSessionUploadTask *uploadTask = nil;
url_session_manager_create_task_safely(^{
uploadTask = [self.session uploadTaskWithRequest:request fromData:bodyData];
});

NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromData:bodyData];

[self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];

return uploadTask;
Expand All @@ -826,10 +788,7 @@ - (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)reques
progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
__block NSURLSessionUploadTask *uploadTask = nil;
url_session_manager_create_task_safely(^{
uploadTask = [self.session uploadTaskWithStreamedRequest:request];
});
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithStreamedRequest:request];

[self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];

Expand All @@ -843,11 +802,8 @@ - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
{
__block NSURLSessionDownloadTask *downloadTask = nil;
url_session_manager_create_task_safely(^{
downloadTask = [self.session downloadTaskWithRequest:request];
});

NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request];

[self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler];

return downloadTask;
Expand All @@ -858,10 +814,7 @@ - (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
{
__block NSURLSessionDownloadTask *downloadTask = nil;
url_session_manager_create_task_safely(^{
downloadTask = [self.session downloadTaskWithResumeData:resumeData];
});
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithResumeData:resumeData];

[self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler];

Expand Down
28 changes: 0 additions & 28 deletions Tests/Tests/AFURLSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,34 +224,6 @@ - (void)testUploadProgressCanBeKVOd {
[self waitForExpectationsWithCommonTimeout];
}

#pragma mark - rdar://17029580

- (void)testRDAR17029580IsFixed {
//https://github.com/AFNetworking/AFNetworking/issues/2093
//https://github.com/AFNetworking/AFNetworking/pull/3205
//http://openradar.appspot.com/radar?id=5871104061079552
dispatch_queue_t serial_queue = dispatch_queue_create("com.alamofire.networking.test.RDAR17029580", DISPATCH_QUEUE_SERIAL);
NSMutableArray *taskIDs = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 100; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:@"Wait for task creation"];
__block NSURLSessionTask *task;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
task = [self.localManager
dataTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL]
uploadProgress:nil
downloadProgress:nil
completionHandler:nil];
dispatch_sync(serial_queue, ^{
XCTAssertFalse([taskIDs containsObject:@(task.taskIdentifier)]);
[taskIDs addObject:@(task.taskIdentifier)];
});
[task cancel];
[expectation fulfill];
});
}
[self waitForExpectationsWithCommonTimeout];
}

#pragma mark - Issue #2702 Tests
// The following tests are all releated to issue #2702

Expand Down

0 comments on commit 67f558f

Please sign in to comment.