Swift3 concurrency

Swift3 concurrency,第1张

概述转自我的github: https://github.com/uniquejava/iOSConcurrencyDemo swift3 concurrency This repo is the steps breaking down from this excellent tutorial and an update for swift3 + xcode8. I seperated each st

转自我的github: https://github.com/uniquejava/iOSConcurrencyDemo

swift3 concurrency

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.

The major difference is on GCD part,for NSOperation part,the changes are minor.

A compact example

Example 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 cheetsheet get main queue

swift2 version:

dispatch_get_main_queue()

swift3 version:

dispatchQueue.main
global concurrent queue

swift2 version:

let queue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0)

swift3 version:

let queue = dispatchQueue.global(qos: .default)
custom serial queue

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.

custom concurrent queue

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)
NSOperationQueue Removed the NS prefix SimplifIEd the method name You can use xcode auto-fix References

http://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

Code

https://github.com/uniquejava/iOSConcurrencyDemo/blob/master/iOSConcurrencyDemo/ViewController.swift

总结

以上是内存溢出为你收集整理的Swift3 concurrency全部内容,希望文章能够帮你解决Swift3 concurrency所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1070635.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存