Browse Questions
  • Encapsulation means bundling data with the operations that act on it, and restricting direct access to that data so internal invariants can't be broken from outside.
  • A read-only computed property has a getter but no setter, so it can be read but never assigned to.
  • OperationQueue is a higher-level abstraction over GCD.
  • A signature proves who sent a payload but says nothing about when, so an attacker who captures a valid signed request can resend it verbatim and it will still verify.

Answer: OperationQueue is a higher-level abstraction over GCD. It adds features like dependencies between operations, cancellation, and maximum concurrency limits.

OperationQueueGCD
Dependencies✅ Yes❌ No
Cancellation✅ YesLimited
Max concurrency✅ YesLimited
KVO observable✅ Yes❌ No
OverheadHigherLower

Code Example:

let queue = OperationQueue()
queue.maxConcurrentOperationCount = 3

let op1 = BlockOperation { print("Download image") }
let op2 = BlockOperation { print("Process image") }
let op3 = BlockOperation { print("Save image") }

// op2 and op3 won't start until op1 finishes
op2.addDependency(op1)
op3.addDependency(op2)

queue.addOperations([op1, op2, op3], waitUntilFinished: false)

// Cancel all
queue.cancelAllOperations()