Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Remove KVO of progress in favor of using the NSURLSession delegate APIs #3607

Merged
merged 1 commit into from Aug 3, 2016

Conversation

coreyfloyd
Copy link
Contributor

The impetus for this change was a bug related to:
https://github.com/AFNetworking/AFNetworking/issues/3380

Which points to an issue where AFURLSessionManager could get in a state where it does not inform the AFURLSessionManagerTaskDelegate to un-observe cancelled (or otherwise completed) tasks before they are deallocated.
While the above issue is specific to background tasks/sessions, we have observed it on sessions with default configurations as well.

Instead of debugging the KVO issues, I decided to replace the KVO code with in favor of the appropriate NSURLSession delegate methods.
Running all unit tests shows that this approach appears to be working.

The pull request that made this change (#3187) mentions that its goal was to remove the need for AFNetworking API consumers to use a KVO API for getting progress.
This makes sense, but I was unable to see any reason why KVO was used internally for getting progress information rather than the NSURLSession Delegate methods.
Because of this, It isn't clear to me what the historical reasons were/are for KVO being needed to satisfy the progress requirements of the AFNetworking API.

With any luck, KVO is no longer needed and we can eliminate this class of crashes by eliminating KVO.

The impetus for this change was a bug related to:
https://github.com/AFNetworking/AFNetworking/issues/3380

Which points to an issue where AFURLSessionManager could get in a state where it does not inform the AFURLSessionManagerTaskDelegate to un-observe cancelled (or otherwise completed) tasks before they are deallocated.
While the above issue is specific to background tasks/sessions, we have observed it on sessions with default configurations as well.

Instead of debugging the KVO issues, I decided to replace the KVO code with in favor of the appropriate NSURLSession delegate methods.
Running all unit tests shows that this approach appears to be working.

The pull request that made this change (AFNetworking#3187) mentions that its goal was to remove the need for AFNetworking API consumers to use a KVO API for getting progress.
This makes sense, but I was unable to see any reason why KVO was used internally for getting progress information rather than the NSURLSession Delegate methods.
Because of this, It isn't clear to me what the historical reasons were/are for KVO being needed to satisfy the progress requirements of the AFNetworking API.

With any luck, KVO is no longer needed and we can eliminate this class of crashes by eliminating KVO.
@coreyfloyd
Copy link
Contributor Author

@kcharwood I'd like to get your thoughts on this PR since I saw you were the author of #3187 which added the KVO.

Is it possible to shed some light on why the delegate methods were removed and replaced with KVO? I assume that either the APIs changed or some of the APIs were unreliable, but the unit tests didn't reveal any issues.

Thanks for looking

@reidmain
Copy link

reidmain commented Aug 2, 2016

What is the status of this PR? Could we get one of the AFNetworking contributors (cc: @kcharwood ) to look over this because I am experiencing crashes caused by this problem.

@kcharwood kcharwood added this to the 3.1.1 milestone Aug 3, 2016
@kcharwood
Copy link
Contributor

Sorry @coreyfloyd for just now looking. Been a busy summer with lots of travel in Swift 3 migrations every two weeks...

I think I originally went the KVO route because of documentation I read somewhere, but I can't seem to find that at the moment...

Looking at the docs now, Apple clearly says to use these delegate callback methods, so I think this is a good approach. I'll pull it down and run the unit tests to verify 👍

@kcharwood kcharwood added the bug label Aug 3, 2016
@kcharwood kcharwood merged commit ff228fa into AFNetworking:master Aug 3, 2016
@kcharwood kcharwood added the fixed label Aug 3, 2016
@coreyfloyd
Copy link
Contributor Author

@kcharwood thanks for looking this over and getting this merged. Sorry I missed your comment earlier. I guess the tests passed for you locally?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants