Skip to content

Commit

Permalink
fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspik committed Nov 12, 2017
1 parent dd59ffa commit c976cf3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion AFNetworking/AFNetworkReachabilityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ NS_ASSUME_NONNULL_BEGIN
*
* @return nil as this method is unavailable
*/
- (nullable instancetype)init NS_UNAVAILABLE;
- (nonnull instancetype)init NS_UNAVAILABLE;

///--------------------------------------------------
/// @name Starting & Stopping Reachability Monitoring
Expand Down
52 changes: 37 additions & 15 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ static dispatch_queue_t url_session_manager_creation_queue() {
}

static void url_session_manager_create_task_safely(dispatch_block_t block) {
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();

if (block != nil) {
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();
}
}
}

Expand Down Expand Up @@ -373,8 +376,7 @@ + (void)load {
7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods
8) Set the current class to the super class, and repeat steps 3-8
*/
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSession * session = [NSURLSession sharedSession];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil];
Expand All @@ -394,7 +396,6 @@ + (void)load {
}

[localDataTask cancel];
[session finishTasksAndInvalidate];
}
}

Expand Down Expand Up @@ -484,8 +485,6 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;

self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];

self.responseSerializer = [AFJSONResponseSerializer serializer];

self.securityPolicy = [AFSecurityPolicy defaultPolicy];
Expand All @@ -499,17 +498,20 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config
self.lock = [[NSLock alloc] init];
self.lock.name = AFURLSessionManagerLockName;

__weak typeof(self) weakSelf = self;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {

__strong typeof(weakSelf) strongSelf = weakSelf;
for (NSURLSessionDataTask *task in dataTasks) {
[self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
[strongSelf addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
}

for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
[self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
[strongSelf addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
}

for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
[strongSelf addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
}
}];

Expand All @@ -522,6 +524,19 @@ - (void)dealloc {

#pragma mark -

- (NSURLSession *)session {

@synchronized (self) {
if (!_session) {
_session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
}
}
return _session;
}

#pragma mark -


- (NSString *)taskDescriptionForSessionTasks {
return [NSString stringWithFormat:@"%p", self];
}
Expand Down Expand Up @@ -683,6 +698,7 @@ - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
} else {
[self.session finishTasksAndInvalidate];
}
self.session = nil;
}

#pragma mark -
Expand Down Expand Up @@ -1058,6 +1074,12 @@ - (void)URLSession:(NSURLSession *)session
if (self.taskDidComplete) {
self.taskDidComplete(session, task, error);
}

if (self.mutableTaskDelegatesKeyedByTaskIdentifier.allKeys.count == 0) {
@synchronized (self) {
[self invalidateSessionCancelingTasks:NO];
}
}
}

#pragma mark - NSURLSessionDataDelegate
Expand Down

0 comments on commit c976cf3

Please sign in to comment.