Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure all the session delegate completionHandler called #2197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dreampiggy
Copy link
Contributor

@dreampiggy dreampiggy commented Jan 28, 2018

This also fix the leak when response error code below iOS 10

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

This merge request fixes / reffers to the following issues: #1953 #1305 #1597

Pull Request Description

Our usage to let SDWebImageDownloader to pass URLSessionDelegate to the operation seems a wrong design. It's because that sometimes, the SDWebImageDownloaderOperation will be a nil. Because in Apple's doc about URLSession life cycle. It does not means that didCompleteWithError: is the final call. But at that time we already removed the SDWebImageDownloaderOperation from the operation queue. So some of delegate methods will not do the correct way like the URLSession behavior, especially some delegate which contains a completionBlock. If you not call that, the URLSession will mass up.

Currently we do not totally change something, but we should add a protect. If the SDWebImageDownloaderOperation is nil, we just use the default behavior in the documentation from Apple.

So for #1953, I found the reason. By the time URLSession:dataTask:willCacheResponse:completionHandler: is called when the server response a 404 error, the SDWebImageDownloaderOperation is already become nil, so this will cause memory leak below iOS 10. Actually Apple already warn you in the doc

If your delegate implements this method, it must call this completion handler; otherwise, your app leaks memory.

2018-01-28 10 35 46

@dreampiggy dreampiggy added the fix label Jan 28, 2018
@dreampiggy dreampiggy added this to the 4.3.0 milestone Jan 28, 2018
@dreampiggy dreampiggy force-pushed the fix_urlcache_behavior_and_leak branch from 4e59d0b to ead067f Compare January 28, 2018 16:02
@codecov-io
Copy link

codecov-io commented Jan 28, 2018

Codecov Report

Merging #2197 into master will decrease coverage by 0.21%.
The diff coverage is 78.94%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2197      +/-   ##
==========================================
- Coverage   78.59%   78.38%   -0.22%     
==========================================
  Files          36       36              
  Lines        3635     3641       +6     
  Branches      322      335      +13     
==========================================
- Hits         2857     2854       -3     
- Misses        756      765       +9     
  Partials       22       22
Impacted Files Coverage Δ
SDWebImage/SDWebImageDownloader.m 81.65% <71.42%> (-1.11%) ⬇️
SDWebImage/SDWebImageDownloaderOperation.m 92.41% <88.23%> (+0.44%) ⬆️
SDWebImage/SDWebImageManager.m 82.38% <0%> (-3.11%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9a123f...9080afd. Read the comment docs.

@dreampiggy dreampiggy force-pushed the fix_urlcache_behavior_and_leak branch 3 times, most recently from 08ac888 to 7f91504 Compare January 29, 2018 06:03
@dreampiggy dreampiggy changed the title Remove the willCacheResponse delegate for URLSession to allow correct cache behavior Ensure all the session delegate completionHandler called Jan 29, 2018
@dreampiggy dreampiggy force-pushed the fix_urlcache_behavior_and_leak branch from 7f91504 to 9080afd Compare January 29, 2018 06:12
} else {
NSUInteger code = ((NSHTTPURLResponse *)response).statusCode;
Copy link
Contributor Author

@dreampiggy dreampiggy Jan 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even through the status code in invalid. We should also store the actual response and post the notification to let user decide the action. Current implementation contains bug.

And also, for 304, it should not directly call cancelInternal, which will cause the completion block not been called at all. Because cancelInternal remove all the callback blocks.

@dreampiggy dreampiggy requested a review from mythodeia January 29, 2018 06:18
Copy link
Contributor

@mythodeia mythodeia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 we should read the Apple docs more often i think 😛

@dreampiggy dreampiggy merged commit 37a062a into SDWebImage:master Jan 30, 2018
@dreampiggy dreampiggy deleted the fix_urlcache_behavior_and_leak branch January 30, 2018 02:51
@bpoplauschi bpoplauschi mentioned this pull request Apr 18, 2018
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants