How to do multithreading, concurrency or parallelism in iOS Swift?

后端 未结 4 1077
梦毁少年i
梦毁少年i 2021-01-30 11:07

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 dela

4条回答
  •  一个人的身影
    2021-01-30 11:25

    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.

提交回复
热议问题