轉自個人github: https://github.com/uniquejava/iOSConcurrencyDemohtml
This repo is the steps breaking down from this excellent tutorial and an update for swift3 + xcode8. I seperated each step into its own commit, you can check the commit history for details.java
The major difference is on GCD part, for NSOperation part, the changes are minor.ios
Example from here qos - new quality of service syntaxgit
weak self - to disrupt retain cyclesgithub
async global background queue - for network queryswift
async main queue - for touching the UI.xcode
Of course you need to add some error checking to this...app
DispatchQueue.global(qos: .background).async { [weak self] () -> Void in self?.flickrPhoto.loadLargeImage { loadedFlickrPhoto, error in if error != nil { print("error:\(error)") } else { DispatchQueue.main.async { () -> Void in activityIndicator.removeFromSuperview() self?.imageView.image = self?.flickrPhoto.largeImage } } } }
swift2 version:async
dispatch_get_main_queue()
swift3 version:ui
DispatchQueue.main
swift2 version:
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
swift3 version:
let queue = DispatchQueue.global(qos: .default)
swift2 version:
let queue = dispatch_queue_create("com.cyper.xxx", DISPATCH_QUEUE_SERIAL)
swift3 version:
let queue = DispatchQueue(label: "com.cyper.xxx")
DispatchQueue by default is serial queue, you don't have to specify it in the initializer.
swift2 version:
let queue = dispatch_queue_create("com.cyper.xxx", DISPATCH_QUEUE_CONCURRENT)
swift3 version:
let queue = DispatchQueue(label: "com.cyper.xxx", qos: .userInitiated, attributes: .concurrent)
NS
prefixhttp://www.appcoda.com/ios-concurrency/
https://medium.com/swift-and-ios-writing/a-quick-look-at-gcd-and-swift-3-732bef6e1838#.ueryj2b2h
https://www.logcg.com/archives/2040.html
http://stackoverflow.com/questions/37805885/how-to-create-dispatch-queue-in-swift-3
https://github.com/uniquejava/iOSConcurrencyDemo/blob/master/iOSConcurrencyDemo/ViewController.swift