Thread-safe singletons and their usage in Swift

  • Usage of singletons, the dos and don’ts
  • Singletons in Swift
  • Concurrency issues
  • Making singletons thread-safe
  • Readers-writer lock

Usage of singletons, the dos and the don’ts

Misusing singletons

Singletons in Swift

Basic implementation of a singleton

public static let shared = EventLogger()

private init() {}

Concurrency issues

An object that manages the execution of tasks serially or concurrently on your app’s main thread or on a background thread.

Making singletons thread-safe

Using serial queue to prevent parallel execution

Readers-writer lock

Added barrier for write operation

--

--

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