Structured Concurrency with Swift

Async/Await

  • Poor error handling because you could not use a single way to handle errors/exceptions instead separate callbacks for errors were needed
  • Difficult to cancel asynchronous operation or exit early after a timeout.
  • Requires a global reasoning of shared state in order to prevent race conditions.
  • Stack traces from the asynchronous thread don’t include the originating request so the code becomes hard to debug.
  • As Swift/Objective-C runtime uses native threads, creating a lot of background tasks results in expensive thread resources and may cause excessive context switching.
  • Nested use of completion handlers turn the code into a callback hell.

Tasks Hierarchy, Priority and Cancellation

Continuations and Scheduling

Multiple Asynchronous Tasks

Task Groups

Data Races

Cancellation

Timeout

Semaphores and Locks

TaskLocal

Detached Tasks (Unstructured)

Legacy APIs

Implementing WebCrawler Using Async/Await

  • Concurrency scope — The async/await defines scope of concurrency where all child background tasks must be completed before returning from the asynchronous function.
  • The async declared methods in above implementation shows asynchronous code can be easily composed.
  • Error handling — Async-await syntax uses normal try/catch syntax for error checking instead of specialized syntax of Promise or callback functions.
  • Swift runtime schedules asynchronous tasks on a fixed number of native threads and automatically suspends tasks when they wait for I/O or other resources.
  • The most glaring omission in above implementation is timeout, which is not supported in Swift’s implementation.
  • Swift runtime manages scheduling of tasks and you cannot pass your own execution dispatcher for scheduling background tasks.

Actors

Priority Inversion Principle

Actor Reentrancy

Actor Isolation

Sendable

  • Value types
  • Actors
  • Immutable classes
  • Synchronized classes
  • @Sendable Functions

@MainActor

@globalActor

Message Pattern Matching

Local only

Actor Executor/Dispatcher

Implementing WebCrawler Using Actors and Tasks

Performance Comparison

--

--

--

Lifelong learner, technologist and a software builder based in Seattle.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The new game-changing feature to look out for in iOS14

didSet, willSet | Property observers in Swift

iOS Interview Questions Part-1

Couch to 5K Runner: A Mobile Forensics Investigation

Style Transfer with Metal

An introduction to UI Testing on iOS

Difference between setNeedsLayout() & layoutIfNeeded()

Using Charles & Mockoon in IOS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
shahzad bhatti

shahzad bhatti

Lifelong learner, technologist and a software builder based in Seattle.

More from Medium

RxSwift: Transforming Operator in Practice

Let’s know about Swift!!

How to create CocoaPods for XCFramework

How to Sync Eagle App Library with Dropbox