Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 746
Martin Cazares Avatar asked Jul 05 '14 18:07

Martin Cazares


People also ask

What is concurrency and multithreading in iOS development?

Concurrency and multithreading are a core part of iOS development. Let's dive into what makes them so powerful, and how we can leverage them in our own Cocoa Touch applications. Concurrency is the notion of multiple things happening at the same time.

How do I run multiple tasks in parallel in Swift?

Thankfully, there’s also tool within the Swift concurrency toolbox that lets us execute a dynamic number of tasks in parallel — task groups. To form a task group, we either call withTaskGroup or withThrowingTaskGroup, depending on whether we’d like to have the option to throw errors within our tasks.

What is concurrency in Swift and why is it important?

One of the benefits of Swift’s built-in concurrency system is that it makes it much easier to perform multiple, asynchronous tasks in parallel, which in turn can enable us to significantly speed up operations that can be broken down into separate parts.

What is multithreading in Java?

Both execution models exhibit multithreading, which is the involvement of multiple threads working towards one common goal. Multithreading is a generalized technique for introducing a combination of concurrency and parallelism into your program.


1 Answers

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.

like image 154
Rob Avatar answered Oct 08 '22 21:10

Rob