Skip to content

Commit

Permalink
Do not dispatch session invalidation on the main thread
Browse files Browse the repository at this point in the history
The dispatching on main thread was introduced in 920e266 in order to fix #2053. It was not a proper fix but rather a lucky coincidence. This kind of KVO error must be fixed by ensuring that observer are properly registered and unregistered, not by dispatching some random method on the main thread.

It is very important not to dispatch async session invalidation code because it is called in unit tests from the `tearDown` method. At that point, there’s no active runloop, so the session invalidation would not happen immediately but when running the next asynchronous unit test, i.e. when the `waitForExpectationsWithCommonTimeoutUsingHandler:` method is called.

This commit, in conjunction with d869571 fixes #3710.
  • Loading branch information
0xced committed Sep 30, 2016
1 parent d869571 commit 2163a8b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,11 @@ - (NSArray *)downloadTasks {
#pragma mark -

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
dispatch_async(dispatch_get_main_queue(), ^{
if (cancelPendingTasks) {
[self.session invalidateAndCancel];
} else {
[self.session finishTasksAndInvalidate];
}
});
if (cancelPendingTasks) {
[self.session invalidateAndCancel];
} else {
[self.session finishTasksAndInvalidate];
}
}

#pragma mark -
Expand Down

0 comments on commit 2163a8b

Please sign in to comment.