Swift3 concurrency
转自我的github: https://github.com/uniquejava/iOSConcurrencyDemo swift3 concurrencyThis 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. The major difference is on GCD part,for NSOperation part,the changes are minor. A compact exampleExample from here qos - new quality of service syntax weak self - to disrupt retain cycles async global background queue - for network query async main queue - for touching the UI. Of course you need to add some error checking to this... 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 GCD cheetsheetget main queueswift2 version: dispatch_get_main_queue() swift3 version: DispatchQueue.main global concurrent queueswift2 version: let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) swift3 version: let queue = DispatchQueue.global(qos: .default) custom serial queueswift2 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. custom concurrent queueswift2 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) NSOperationQueue
Referenceshttp://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 Codehttps://github.com/uniquejava/iOSConcurrencyDemo/blob/master/iOSConcurrencyDemo/ViewController.swift (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 什么是开发Flash应用程序的最佳开源工具?什么是开始学习的
- 什么是swift 3中的NSTimer.scheduledTimerWithTimeInterval
- ruby-on-rails – 使用Ruby 1.9.2缓慢部署大型Rails 3应用程
- flex air 设置应用程序图标 - [Desktop - AIR]
- 如何在Swift中创建唯一对象列表的数组
- c – 为什么sprintf_s在不同版本的Visual Studio中给出不同
- c – 引用类型返回函数和后缀增量
- xml – 如何向Facebook提交错误报告?
- Oracle的数据表中行转列与列转行的操作实例讲解
- c# – 如果xmldocument中存在属性,则删除该属性