Exploring Singleton Design Patterns in iOS
π Introduction
The Singleton design pattern is a creational pattern that ensures that only one instance of a class can be created and used throughout the lifetime of an application.
This is useful in scenarios where you need to ensure that a single,...
theiosmentor.hashnode.dev4 min read
This is a wrong assumption. Initialization is not thread-safe, the initialization block can be called multiple times in a concurrent environment. For example:
class Example { lazy var value: Int = { print("Initializing value") return 42 }() } let instance = Example() DispatchQueue.global().async { print("Thread 1: \(instance.value)") } DispatchQueue.global().async { print("Thread 2: \(instance.value)") }"Initializing value" will be printed twice. See also this discussion - forums.swift.org/t/lazy-property-initialization-question/51085/8 for deeper understanding.