Skip to content
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

move queue dispatch code into ImageDownloader #238

Merged
merged 2 commits into from
Feb 17, 2016
Merged

move queue dispatch code into ImageDownloader #238

merged 2 commits into from
Feb 17, 2016

Conversation

dopcn
Copy link
Contributor

@dopcn dopcn commented Feb 16, 2016

实现的效果仍然是当不显式指定 callbackQueue 时 completionHandler 在 main queue 上调用,显示指定 callbackQueue 则在指定 queue 上运行,无论何时 progressBlock 都在 main queue 上调用

原本在 ImageDownloader 里 session object 的 delegateQueue 是 main queue,所以可能是这个原因导致了会出现在 main queue 调用 dispatch_async(dispatch_get_main_queue(), ...) 的情况,例如最终调用 completionHandler 的是函数
private func callbackWithImage(image: Image?, error: NSError?, imageURL: NSURL, originalData: NSData?) {}

这个函数在原本的实现中有可能在 processQueue 上被调用
(函数 processImageForTask 的实现),
也有可能在 main queue 上被调用
URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) 的实现)

在 main queue 上被调用,在 handler 里又包含 dispatch_async(dispatch_get_main_queue(), ...) 就会导致 reload 时出现闪动。

新的实现将 session object 的 delegateQueue 改为自定义的 operation queue 所有的 delegate method 都在这个 queue 上运行,需要调用 progressBlock 或 completionHandler 的时候运用 dispatch_async(options.callbackDispatchQueue, ...) 发送到默认的 main queue 或者指定的 callbackQueue 上去,这样应该可以杜绝出现在 main queue 调用 dispatch_async(dispatch_get_main_queue(), ...) 的情况。

这一点没法完全通过测试来证明,还需要喵神 review 看看是否是这样

@codecov-io
Copy link

Current coverage is 75.55%

Merging #238 into master will not affect coverage as of 402931c

@@            master    #238   diff @@
======================================
  Files           11      11       
  Stmts         1252    1252       
  Branches         0       0       
  Methods          0       0       
======================================
  Hit            946     946       
  Partial          0       0       
  Missed         306     306       

Review entire Coverage Diff as of 402931c

Powered by Codecov. Updated on successful CI builds.

@onevcat
Copy link
Owner

onevcat commented Feb 16, 2016

I had a brief look at it just now and the first impression is cool. I will check how it would behave in detail later today.

Thanks for contributing!

@onevcat onevcat merged commit 33a1025 into onevcat:master Feb 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants