Using a dispatch_once singleton model in Swift
tl;dr: Use the class constant approach if you are using Swift 1.2 or above and the nested struct approach if you need to support earlier versions.
From my experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety.
Class constant
class Singleton {
static let sharedInstance = Singleton()
}
This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of let
. This is now officially recommended way to instantiate a singleton.
Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.
Nested struct
class Singleton {
class var sharedInstance: Singleton {
struct Static {
static let instance: Singleton = Singleton()
}
return Static.instance
}
}
Here we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions.
dispatch_once
The traditional Objective-C approach ported to Swift. I'm fairly certain there's no advantage over the nested struct approach but I'm putting it here anyway as I find the differences in syntax interesting.
class Singleton {
class var sharedInstance: Singleton {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: Singleton? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = Singleton()
}
return Static.instance!
}
}
See this GitHub project for unit tests.
Swift singleton that does not look like a singleton
In Objective-C an initialiser is just like any other method that is called on an instance
[[alloc SomeClass] init]
You first alloc
an instance and then explicitly invoke its initialiser.
init
is able to return any object; returning self
is just a convention that can be ignored in special cases, such as the one you have shown.
In Swift, init
is special. An initialiser is invoked implicitly as part of the allocation
let x = SomeClass()
A Swift init
implicitly returns self
(or it can return nil
in the case of failable initialiser that has failed). As a result, you cannot implement a "hidden singleton" in Swift by returning some other object.
dispatch_once singleton in Swift
This is from this Ray Wenderlich tutorial, right? Swift doesn't have static variables that can be scoped to functions, but you can nest a type inside a function, and give it static variable. Here's a Swift equivalent version of the start of that method:
func heightForBasicCellAtIndexPath(indexPath: NSIndexPath) -> CGFloat {
struct Static {
static var sizingCell: RWBasicCell?
}
if Static.sizingCell == nil {
Static.sizingCell = tableView.dequeueReusableCellWithIdentifier(RWBasicCellIdentifier) as RWBasicCell
}
// ...
}
Create singleton using GCD's dispatch_once in Objective-C
This is a perfectly acceptable and thread-safe way to create an instance of your class. It may not technically be a "singleton" (in that there can only ever be 1 of these objects), but as long as you only use the [Foo sharedFoo]
method to access the object, this is good enough.
How to Implement Singleton class in Swift
It's so simple in Swift:
class YourClass {
static let sharedInstance = YourClass()
}
and to use it:
YourClass.sharedInstance
dispatch_once_t alternative in swift
In swift singleton can be written as,
class Model: NSObject {
static let sharedInstance = Model()
}
then use Model.sharedInstance
. you dont need dispatch once like in objective c.
source https://thatthinginswift.com/singletons/
Where Singleton object is allocated?
Swift allocates storage for
MyClass.shared
in the data segment, initialized to nil. The data segment's layout and initial contents are defined by the executable file. Historically, the heap started immediately at the end of the data segment, but on modern 64-bit systems with address space layout randomization (ASLR), I don't know if that's still true.Swift also allocates a
swift_once_t
in the data segment to record whetherMyClass.shared
has been initialized yet.Swift generates a getter function for
MyClass.shared
in the code segment. The getter function uses theswift_once_t
to initialize the storage ofMyClass.shared
the first time the getter is called. It looks approximately like this:var _storage_MyClass_shared: MyClass? = nil
var _once_MyClass_shared: swift_once_t = .init() // essentially, false
func _getter_MyClass_shared() -> MyClass {
swift_once(&_once_MyClass_shared, {
_storage_MyClass_shared = MyClass()
})
return _storage_MyClass_shared!
}The instance of
MyClass
is stored on the heap. It starts with a word containing theisa
pointer (to theMyClass
metadata), followed by a word containing (usually) reference counts, followed by storage for the object's instance variables. In your case, there are no instance variables, so there is no additional storage. The blue box labeledMyclass()
in your diagram, and the arrow pointing to it, do not exist.If
myClass
is at top-level (not inside a method or data type declaration), then it is also stored in the data segment along with anotherswift_once_t
that tracks whether it's been initialized, and Swift generates a getter for it in the code segment.If
myClass
is an instance variable of a data type, then it is stored as part of its containing object, which may be either on the stack or the heap (in the case of astruct
,enum
, or tuple) or always on the heap (in the case of aclass
oractor
).If
myClass
is a local variable in a function, then it is stored on the stack.
Whither dispatch_once in Swift 3?
Since Swift 1.x, Swift has been using dispatch_once
behind the scenes to perform thread-safe lazy initialization of global variables and static properties.
So the static var
above was already using dispatch_once
, which makes it sort of weird (and possibly problematic to use it again as a token for another dispatch_once
. In fact there's really no safe way to use dispatch_once
without this kind of recursion, so they got rid of it. Instead, just use the language features built on it:
// global constant: SomeClass initializer gets called lazily, only on first use
let foo = SomeClass()
// global var, same thing happens here
// even though the "initializer" is an immediately invoked closure
var bar: SomeClass = {
let b = SomeClass()
b.someProperty = "whatever"
b.doSomeStuff()
return b
}()
// ditto for static properties in classes/structures/enums
class MyClass {
static let singleton = MyClass()
init() {
print("foo")
}
}
So that's all great if you've been using dispatch_once
for one-time initialization that results in some value -- you can just make that value the global variable or static property you're initializing.
But what if you're using dispatch_once
to do work that doesn't necessarily have a result? You can still do that with a global variable or static property: just make that variable's type Void
:
let justAOneTimeThing: () = {
print("Not coming back here.")
}()
And if accessing a global variable or static property to perform one-time work just doesn't feel right to you -- say, you want your clients to call an "initialize me" function before they work with your library -- just wrap that access in a function:
func doTheOneTimeThing() {
justAOneTimeThing
}
See the migration guide for more.
Related Topics
Swift: How to Add a Protocol Extension to a Protocol
Number of Words in a Swift String For Word Count Calculation
Usb Connection Delegate on Swift
Swift - Extra Argument in Call
Why Is 'Nil' Not Compatible With 'Unsafepointer≪Cgaffinetransform≫' in Swift 3
Is This Response from the Compiler Valid
Constant Unassigned Optional Will Not Be Nil by Default
Get Associated Value from Enumeration Without Switch/Case
How to Pass Multiple Enum Values as a Function Parameter
Swift Variable Name With ' (Backtick)
Xcode 8 Beta 3: Expected ',' Joining Parts of a Multi-Clause Condition
Weak References in Swift Playground Don't Work as Expected
Deletable Table With Textfield on Swiftui
Storing Values in Completionhandlers - Swift
In Swiftui, How to Increase the Height of a Button
Delete Folder With Contents from Firebase Storage
Fatal Error: Swapping a Location With Itself Is Not Supported With Swift 2.0