swift 实例演示 Operation 的用法

swift 实例演示 Operation 的用法,第1张

概述前言 本文代码虽是手动code了一遍并小小做了改动,但是终究是在他人demo的基础上编排出的,即便是个比较简单的例子,但是这个..那个..为了尊重别人劳动成果,还是分类到了转载,这里特别感谢一下@非典型技术宅老兄的原文,想必大家都听腻了太多的多线程的概念理论,本文不大书理论,用实例讲述 Operation Queues 的用法,就是这么任性! 并行执行任务,全部完成后刷新UI 需求: 1.分线程下 前言

本文代码虽是手动code了一遍并小小做了改动,但是终究是在他人demo的基础上编排出的,即便是个比较简单的例子,但是这个..那个..为了尊重别人劳动成果,还是分类到了转载,这里特别感谢一下@非典型技术宅老兄的原文,想必大家都听腻了太多的多线程的概念理论,本文不大书理论,用实例讲述Operation Queues 的用法,就是这么任性!


并行执行任务,全部完成后刷新UI

需求:

1.分线程下载图片并显示

2.下载过程中显示loading

3.全部下载完成后停止loading


代码:

let imgW = Int(UIScreen.main.bounds.wIDth - 20)        let imgH = Int((UIScreen.main.bounds.height - 80)/4)        @IBOutlet weak var indicator: UIActivityIndicatorVIEw!        @IBOutlet weak var img1: UIImageVIEw!        @IBOutlet weak var img2: UIImageVIEw!        @IBOutlet weak var img3: UIImageVIEw!        @IBOutlet weak var img4: UIImageVIEw!        let operationQueue = OperationQueue()        var imageVIEws: [UIImageVIEw]?        var operationType: OperationType?        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        imageVIEws = [img1,img2,img3,img4]    }        // actions    @IBAction func rightItemAction(_ sender: Any) {        indicator.startAnimating()        startBasicDemo()    }    func startBasicDemo() {        // 最大并行数 3        operationQueue.maxConcurrentoperationCount = 3        // 添加任务下载图片  在主线程刷新UI        for imageVIEw in imageVIEws! {            operationQueue.addOperation {                if let url = URL(string: "https://placebeard.it/\(self.imgW)/\(self.imgH)") {                    do {                        let image = UIImage(data:try Data(contentsOf: url))                        dispatchQueue.main.async {                            imageVIEw.image = image                        }                    } catch {                        print(error)                    }                }            }        }        // 等待所有 *** 作完成,回到主线程停止刷新器        dispatchQueue.global().async {            [weak self] in            self?.operationQueue.waitUntilAllOperationsAreFinished()            dispatchQueue.main.async {                self?.indicator.stopAnimating()            }        }    }

解析:

operationQueue: *** 作队列

operationQueue.addOperation:向队列中添加任务(例子中放在for循环中,添加了多个任务)

do{} catch{}:执行任务,捕获异常

dispatchQueue.global().async:全局队列异步执行

dispatchQueue.main.async:主队列异步执行


设置队列中的优先级并执行异步任务

需求:

1.分线程下载图片并显示

2.下载过程中显示loading

3.全部下载完成后停止loading

4.设置每个任务的优先级


代码:

class ConvenIEnceOperation: Operation {    let url: URL    let imageVIEw: UIImageVIEw        init(setimageVIEw: UIImageVIEw,withURL: URL) {        imageVIEw = setimageVIEw        url = withURL        super.init()    }        overrIDe func main() {        do {            // 当任务被取消的时候,立刻返回            if isCancelled {                return            }            let imageData = try Data(contentsOf: url)            let image = UIImage(data: imageData)                        dispatchQueue.main.async {                [weak self] in                self?.imageVIEw.image = image            }        } catch  {            print(error)        }    }}
func startDependencyDemo() {        operationQueue.maxConcurrentoperationCount = 4        if let url = URL(string: "https://placebeard.it/\(self.imgW)/\(self.imgH)") {            let operation1 = ConvenIEnceOperation(setimageVIEw: img1,withURL: url)            let operation2 = ConvenIEnceOperation(setimageVIEw: img2,withURL: url)            let operation3 = ConvenIEnceOperation(setimageVIEw: img3,withURL: url)            let operation4 = ConvenIEnceOperation(setimageVIEw: img4,withURL: url)            // 设置依赖关系 执行顺序为 4,3,2,1            operation1.addDependency(operation2)            operation2.addDependency(operation3)            operation3.addDependency(operation4)                        // 等待所有 *** 作完成,回到主线程停止刷新器。            dispatchQueue.global().async {                [weak self] in                self?.operationQueue.addOperations([operation1,operation2,operation3,operation4],waitUntilFinished: true)                dispatchQueue.main.async {                    self?.indicator.stopAnimating()                }            }        }    }

解析:

queuConverIEnceOperation:封装下载图片的任务

queuePriority:任务的优先级(有高到低依次为:veryHigh、high、normal、low、veryLow),这里的优先级值得是对某一任务分配资源的优先级,由于这里设置的

maxConcurrentoperationCount(最大并行数)为2,并且任务放在异步队列里,所以看上去任务并没有按从高到低的顺序执行,如果想实现按顺序执行任务,只需将并行数设置为1,或者对任务设置依赖关系,下文会讲解到。


为队列中的任务设置依赖并异步执行任务

需求:

1.分线程下载图片并显示

2.下载过程中显示loading

3.全部下载完成后停止loading

4.任务间添加依赖关系


代码:

func startDependencyDemo() {        operationQueue.maxConcurrentoperationCount = 4        if let url = URL(string: "https://placebeard.it/\(self.imgW)/\(self.imgH)") {            let operation1 = ConvenIEnceOperation(setimageVIEw: img1,waitUntilFinished: true)                dispatchQueue.main.async {                    self?.indicator.stopAnimating()                }            }        }    }

解析:

operation1.addDependency(operation2):operation1依赖operation2,既任务2完成后再执行任务1。


总结 operation的属性、方法: 总结

以上是内存溢出为你收集整理的swift 实例演示 Operation 的用法全部内容,希望文章能够帮你解决swift 实例演示 Operation 的用法所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1056251.html

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

发表评论

登录后才能评论

评论列表(0条)

保存