如何在iOS Swift中做多线程,并发或并行? [英] How to do multithreading, concurrency or parallelism in iOS Swift?

查看:813
本文介绍了如何在iOS Swift中做多线程,并发或并行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在Swift?中创建一个工作线程,例如,如果一个主要的功能需要大量的计算,因此导致主线程延迟几秒钟,如果我想移动功能到一个单独的线程或线程,不阻塞主线程有什么办法用Swift?



我已经遍历了Swift的Apple文档的基本和高级组件,但没有关于并发或并行的知识,任何人都知道如何做到这一点可能)?

解决方案

或者也可以使用操作队列。在Swift 3中:

  let queue = OperationQueue()

queue.addOperation(){
//在后台执行某些操作

OperationQueue.main.addOperation(){
//完成后,更新主队列上的UI和/或模型
}
}

这是或GCD,其中说明了,工作正常。



请参阅Apple的并发编程指南。虽然该指南仍然说明使用Objective-C的示例,但是API和概念在Swift中基本相同(只使用Swift语法)。 Xcode中的GCD和操作队列的文档描述了Objective-C和Swift API。






顺便说一句,我们注意到在上面的例子以及Andy的GCD演示中,我们使用尾随闭包。例如,如果你看一下定义 addOperationWithBlock ,那就定义为一个函数,其中一个参数是一个closure(类似于Objective-C中的一个块) ):

  func addOperation(_ block:@escaping() - > Swift.Void)

这可能会导致您假设您将按如下方式调用它:

  queue.addOperation({
//在后台执行某些操作
})

但是当函数的最后一个参数是一个闭包时,尾随闭合语法允许你从函数的括号中取出最后一个闭合参数,并将它移动到函数后面,产生:

  queue.addOperation(){
//在后台执行某些操作
}

因为括号里没有什么,你甚至可以进一步,删除那些空括号:

  queue.addOperation {
//在后台执行某些操作
}
<希望这说明如何解释 NSOperationQueue /

> OperationQueue
和/或GCD函数声明,并在代码中使用它们。


Is there any way to create a worker thread in Swift?, for example, if there's a major functionality that requires a lot of calculations and hence causes the main thread to delay for a few seconds, if I would like to move that functionality to a separate thread or a thread that do not block the main thread is there any way to do it with Swift?

I've gone through the basic and advanced components of the Apple Documentation for Swift but there's nothing about concurrency or parallelism, do anyone know something about how to do it(if possible)?

解决方案

Or you can use operation queues, too. In Swift 3:

let queue = OperationQueue()

queue.addOperation() {
    // do something in the background

    OperationQueue.main.addOperation() {
        // when done, update your UI and/or model on the main queue
    }
}

Either this, or GCD, which Andy illustrated, work fine.

See Apple's Concurrency Programming Guide for the relative merits of operation queues and dispatch queues (aka Grand Central Dispatch, GCD). While that guide is still illustrating the examples using Objective-C, the API and concepts are basically the same in Swift (just use the Swift syntax). The documentation for GCD and operation queues in Xcode describes both Objective-C and Swift APIs.


By the way, you'll notice that in both the above example as well as Andy's GCD demonstration, we used "trailing closures". For example, if you look at the definition addOperationWithBlock, that is defined as a function with one parameter which is a "closure" (which is analogous to a block in Objective-C):

func addOperation(_ block: @escaping () -> Swift.Void)

That might lead you to assume that you would invoke it as follows:

queue.addOperation({
    // do something in the background
})

But when the last parameter of a function is a closure, the trailing closure syntax allows you to take that final closure parameter out of the parentheses of the function, and move it after the function, yielding:

queue.addOperation() {
    // do something in the background
}

And because there's nothing left in the parentheses, you can even go one step further, and remove those empty parentheses:

queue.addOperation {
    // do something in the background
}

Hopefully that illustrates how to interpret the NSOperationQueue/OperationQueue and/or GCD function declarations and use them in your code.

这篇关于如何在iOS Swift中做多线程,并发或并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆