O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the addOperation() method of the operation queue

The OperationQueue class has a method named addOperation(), which makes it easy to add a block of code to the queue. This method automatically wraps the block of code in an operation object and then passes that operation to the queue. Let's see how to use this method to add tasks to a queue:

let operationQueue = OperationQueue() 
let calculation = DoCalculations()operationQueue.addOperation() { 
  calculation.performCalculation(10000000, tag: "Operation1") 
} 
operationQueue.addOperation() { 
  calculation.performCalculation(10000, tag: "Operation2") 
} 
operationQueue.addOperation() { 
  calculation.performCalculation(1000000, tag: "Operation3") 
} 

In the BlockOperation example, earlier in this chapter, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required