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

28,505

Solution 1

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.

Solution 2

You can use Grand Central Dispatch (GCD) for such tasks.

This is a basic example:

let backgroundQueue: dispatch_queue_t = dispatch_queue_create("com.a.identifier", DISPATCH_QUEUE_CONCURRENT)

// can be called as often as needed
dispatch_async(backgroundQueue) {
    // do calculations
}

// release queue when you are done with all the work
dispatch_release(backgroundQueue)
Share:
28,505
Martin Cazares
Author by

Martin Cazares

val profile = getCurrentWebsiteProfile() if (profile is LinkedInProfile) { print("${profile.getAwesomeProfile()}") }else { print("${getBlogUrl()}") } http://www.doepiccoding.com/blog/?cat=10.

Updated on October 05, 2020

Comments

  • Martin Cazares
    Martin Cazares over 3 years

    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)?