move queue dispatch code into ImageDownloader #238
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
实现的效果仍然是当不显式指定 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 看看是否是这样