在Swift中使用dispatch_once单例模型

问题描述 投票:553回答:31

我正在尝试找出适合在Swift中使用的单例模型。到目前为止,我已经能够得到一个非线程安全模型:

class var sharedInstance:TPScopeManager {
    get {
        struct Static {
            static var instance : TPScopeManager? = nil
        }

        if !Static.instance {
            Static.instance = TPScopeManager()
        }

        return Static.instance!
    }
}

在Static结构中包装单例实例应该允许单个实例在没有复杂命名方案的情况下不与单例实例发生冲突,并且它应该使事情变得相当私密。显然,这个模型不是线程安全的,所以我尝试将dispatch_once添加到整个事情中:

class var sharedInstance:TPScopeManager {
    get {
        struct Static {
            static var instance : TPScopeManager? = nil
            static var token : dispatch_once_t = 0
        }

        dispatch_once(Static.token) { Static.instance = TPScopeManager() }

        return Static.instance!
    }
}

但是我在dispatch_once行上遇到编译器错误:

无法将表达式的类型'Void'转换为'()'类型

我已经尝试了几种不同的语法变体,但它们似乎都有相同的结果:

dispatch_once(Static.token, { Static.instance = TPScopeManager() })

使用Swift的dispatch_once的正确用法是什么?我最初认为问题出在块中,因为错误消息中的(),但是我看的越多,我认为这可能是正确定义dispatch_once_t的问题。

swift singleton dispatch
31个回答
698
投票

tl; dr:如果使用Swift 1.2或更高版本,则使用类常量方法,如果需要支持早期版本,则使用嵌套结构方法。

根据我使用Swift的经验,有三种方法可以实现支持延迟初始化和线程安全的Singleton模式。

Class constant

class Singleton  {
   static let sharedInstance = Singleton()
}

这种方法支持延迟初始化,因为Swift懒惰地初始化类常量(和变量),并且通过let的定义是线程安全的。现在这是officially recommended way实例化一个单身人士。

类常量是在Swift 1.2中引入的。如果需要支持早期版本的Swift,请使用下面的嵌套结构方法或全局常量。

Nested struct

class Singleton {
    class var sharedInstance: Singleton {
        struct Static {
            static let instance: Singleton = Singleton()
        }
        return Static.instance
    }
}

这里我们使用嵌套结构的静态常量作为类常量。这是Swift 1.1及更早版本中缺少静态类常量的一种解决方法,并且仍然可以作为函数中缺少静态常量和变量的解决方法。

dispatch_once

传统的Objective-C方法移植到Swift。我相当确定没有优于嵌套结构方法的优势,但无论如何我都把它放在这里,因为我发现语法上的差异很有趣。

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!
    }
}

请参阅此GitHub项目进行单元测试。


5
投票

在信中,

class Manager {
    static let sharedInstance = Manager()
    private init() {}
}

你可能想读Files and Initialization

全局变量(也用于结构和枚举的静态成员)的延迟初始化程序在第一次访问全局时运行,并作为dispatch_once启动,以确保初始化是原子的。


4
投票

如果您计划在Objective-C中使用Swift单例类,则此设置将使编译器生成适当的类似Objective-C的标头:

class func sharedStore() -> ImageStore {
struct Static {
    static let instance : ImageStore = ImageStore()
    }
    return Static.instance
}

然后在Objective-C课程中,你可以像在Swift之前的日子里那样调用你的单身人士:

[ImageStore sharedStore];

这只是我简单的实现。


4
投票

第一解决方案

let SocketManager = SocketManagerSingleton();

class SocketManagerSingleton {

}

稍后在您的代码中:

func someFunction() {        
    var socketManager = SocketManager        
}

二解决方案

func SocketManager() -> SocketManagerSingleton {
    return _SocketManager
}
let _SocketManager = SocketManagerSingleton();

class SocketManagerSingleton {

}

稍后在您的代码中,您将能够保持大括号以减少混淆:

func someFunction() {        
    var socketManager = SocketManager()        
}

4
投票
final class MySingleton {
     private init() {}
     static let shared = MySingleton()
}

然后打电话给它;

let shared = MySingleton.shared

4
投票

使用:

class UtilSingleton: NSObject {

    var iVal: Int = 0

    class var shareInstance: UtilSingleton {
        get {
            struct Static {
                static var instance: UtilSingleton? = nil
                static var token: dispatch_once_t = 0
            }
            dispatch_once(&Static.token, {
                Static.instance = UtilSingleton()
            })
            return Static.instance!
        }
    }
}

如何使用:

UtilSingleton.shareInstance.iVal++
println("singleton new iVal = \(UtilSingleton.shareInstance.iVal)")

4
投票

Swift 1.2以上的最佳方法是单行单例,如 -

class Shared: NSObject {

    static let sharedInstance = Shared()

    private override init() { }
}

要了解有关此方法的更多详细信息,您可以访问此link


3
投票

我建议使用Enum,就像你在Java中使用的那样,例如:

enum SharedTPScopeManager: TPScopeManager {
  case Singleton
}

3
投票

来自Apple Docs(Swift 3.0.1),

您可以简单地使用静态类型属性,即使在同时跨多个线程访问时,也可以保证只延迟初始化一次:

class Singleton {
    static let sharedInstance = Singleton()
}

如果需要在初始化之外执行其他设置,则可以将闭包调用的结果分配给全局常量:

class Singleton {
    static let sharedInstance: Singleton = {
        let instance = Singleton()
        // setup code
        return instance
    }()
}

2
投票

仅供参考,这是Jack Wu / hpique的嵌套结构实现的单例实现。该实现还显示了归档如何工作,以及一些附带的功能。我找不到这个完整的例子,所以希望这有助于某人!

import Foundation

class ItemStore: NSObject {

    class var sharedStore : ItemStore {
        struct Singleton {
            // lazily initiated, thread-safe from "let"
            static let instance = ItemStore()
        }
        return Singleton.instance
    }

    var _privateItems = Item[]()
    // The allItems property can't be changed by other objects
    var allItems: Item[] {
        return _privateItems
    }

    init() {
        super.init()
        let path = itemArchivePath
        // Returns "nil" if there is no file at the path
        let unarchivedItems : AnyObject! = NSKeyedUnarchiver.unarchiveObjectWithFile(path)

        // If there were archived items saved, set _privateItems for the shared store equal to that
        if unarchivedItems {
            _privateItems = unarchivedItems as Array<Item>
        } 

        delayOnMainQueueFor(numberOfSeconds: 0.1, action: {
            assert(self === ItemStore.sharedStore, "Only one instance of ItemStore allowed!")
        })
    }

    func createItem() -> Item {
        let item = Item.randomItem()
        _privateItems.append(item)
        return item
    }

    func removeItem(item: Item) {
        for (index, element) in enumerate(_privateItems) {
            if element === item {
                _privateItems.removeAtIndex(index)
                // Delete an items image from the image store when the item is 
                // getting deleted
                ImageStore.sharedStore.deleteImageForKey(item.itemKey)
            }
        }
    }

    func moveItemAtIndex(fromIndex: Int, toIndex: Int) {
        _privateItems.moveObjectAtIndex(fromIndex, toIndex: toIndex)
    }

    var itemArchivePath: String {
        // Create a filepath for archiving
        let documentDirectories = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        // Get the one document directory from that list
        let documentDirectory = documentDirectories[0] as String
        // append with the items.archive file name, then return
        return documentDirectory.stringByAppendingPathComponent("items.archive")
    }

    func saveChanges() -> Bool {
        let path = itemArchivePath
        // Return "true" on success
        return NSKeyedArchiver.archiveRootObject(_privateItems, toFile: path)
    }
}

如果你不认识其中的一些功能,这里有一个我一直在使用的生活Swift实用文件:

import Foundation
import UIKit

typealias completionBlock = () -> ()

extension Array {
    func contains(#object:AnyObject) -> Bool {
        return self.bridgeToObjectiveC().containsObject(object)
    }

    func indexOf(#object:AnyObject) -> Int {
        return self.bridgeToObjectiveC().indexOfObject(object)
    }

    mutating func moveObjectAtIndex(fromIndex: Int, toIndex: Int) {
        if ((fromIndex == toIndex) || (fromIndex > self.count) ||
            (toIndex > self.count)) {
                return
        }
        // Get object being moved so it can be re-inserted
        let object = self[fromIndex]

        // Remove object from array
        self.removeAtIndex(fromIndex)

        // Insert object in array at new location
        self.insert(object, atIndex: toIndex)
    }
}

func delayOnMainQueueFor(numberOfSeconds delay:Double, action closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue()) {
            closure()
    }
}

2
投票

唯一正确的方法如下

final class Singleton {
    static let sharedInstance: Singleton = {
        let instance = Singleton()
        // setup code if anything
        return instance
    }()

    private init() {}
}

访问

let signleton = Singleton.sharedInstance

原因:

  • 静态类型属性保证只是懒惰地初始化一次,即使在同时跨多个线程访问时也是如此,因此不需要使用dispatch_once
  • 私有化init方法,因此其他类不能创建实例。
  • final类,因为您不希望其他类继承Singleton类

173
投票

既然Apple已经澄清了静态结构变量的初始化既懒又包裹在dispatch_once中(参见帖子末尾的注释),我认为我的最终解决方案将是:

class WithSingleton {
    class var sharedInstance :WithSingleton {
        struct Singleton {
            static let instance = WithSingleton()
        }

        return Singleton.instance
    }
}

这利用了静态结构元素的自动惰性,线程安全初始化,安全地隐藏了消费者的实际实现,保持所有紧凑区分以便易读,并消除了可见的全局变量。

Apple澄清了懒惰的初始化程序是线程安全的,所以不需要dispatch_once或类似的保护

全局变量(也适用于结构和枚举的静态成员)的惰性初始化程序在第一次访问全局时运行,并作为dispatch_once启动,以确保初始化是原子的。这样就可以在代码中使用dispatch_once:只需使用初始化程序声明一个全局变量并将其标记为私有。

来自here


1
投票

我更喜欢这个实现:

class APIClient {

}

var sharedAPIClient: APIClient = {
    return APIClient()
}()

extension APIClient {
    class func sharedClient() -> APIClient {
        return sharedAPIClient
    }
}

1
投票

我在Swift中的实现方式......

ConfigurationManager.swift

import Foundation

    let ConfigurationManagerSharedInstance = ConfigurationManager()
 class ConfigurationManager : NSObject {
    var globalDic: NSMutableDictionary = NSMutableDictionary()

class var sharedInstance:ConfigurationManager {
    return ConfigurationManagerSharedInstance

}

init() {

    super.init()

    println ("Config Init been Initiated, this will be called only onece irrespective of many calls")   

}

通过以下方式从应用程序的任何屏幕访问globalDic。

读:

 println(ConfigurationManager.sharedInstance.globalDic)  

写:

 ConfigurationManager.sharedInstance.globalDic = tmpDic // tmpDict is any value that to be shared among the application

1
投票

在看到David的实现之后,似乎没有必要使用单例类函数instanceMethod,因为let与sharedInstance类方法几乎完全相同。你需要做的就是将它声明为一个全局常量,就是这样。

let gScopeManagerSharedInstance = ScopeManager()

class ScopeManager {
 // No need for a class method to return the shared instance. Use the gScopeManagerSharedInstance directly. 
}

1
投票

在swift中,您可以按照以下方式创建单例类:

class AppSingleton: NSObject {

    //Shared instance of class
    static let sharedInstance = AppSingleton()

    override init() {
        super.init()
    }
}

0
投票
   func init() -> ClassA {
    struct Static {
        static var onceToken : dispatch_once_t = 0
        static var instance : ClassA? = nil
    }

    dispatch_once(&Static.onceToken) {
        Static.instance = ClassA()
    }

    return Static.instance!
}

0
投票

Swift在过去实现单例,只不过是三种方式:全局变量,内部变量和dispatch_once方式。

这里有两个很好的单例。(注意:无论何种写法都必须注意init()方法的私有化。因为在Swift中,所有对象的构造函数默认都是public,需要重写init才能变为私有,防止此类'()'的其他对象默认初始化方法创建对象。)

方法1:

class AppManager {
    private static let _sharedInstance = AppManager()

    class func getSharedInstance() -> AppManager {
       return _sharedInstance
    }

    private init() {} // Privatizing the init method
}

// How to use?
AppManager.getSharedInstance()

方法2:

class AppManager {
    static let sharedInstance = AppManager()

    private init() {} // Privatizing the init method
}

// How to use?
AppManager.sharedInstance

-1
投票

我刚刚遇到过这个,但是我需要我的单例来允许继承,而这些解决方案都没有实际允许它。

所以我想出了这个:

public class Singleton {
  private static var sharedInstanceVar = Singleton()

  public class func sharedInstance()->Singleton {
    return sharedInstanceVar
  }
}


public class SubSingleton: Singleton {

  private static var sharedInstanceToken:dispatch_once_t = 0

  public class override func sharedInstance()->SubSingleton {
    dispatch_once(&sharedInstanceToken){
      sharedInstanceVar = SubSingleton()
    }
    return sharedInstanceVar as! SubSingleton
  }
}
  • 这种方式首先执行Singleton.sharedInstance()时会返回Singleton的实例
  • 首先执行SubSingleton.sharedInstance()时,它将返回创建的SubSingleton实例。
  • 如果完成上述操作,则SubSingleton.sharedInstance()为Singleton为true且使用相同的实例。

第一个脏方法的问题是我不能保证子类将实现dispatch_once_t并确保每个类只修改一次sharedInstanceVar ...

我会尝试进一步改进这一点,但是看看是否有人对此有强烈的感受(除了它是冗长的并且需要手动更新它)这将是有趣的。


-1
投票

这是最简单的具有线程安全功能的。没有其他线程可以访问相同的单例对象,即使他们想要。斯威夫特3/4

struct DataService {

    private static var _instance : DataService?

    private init() {}   //cannot initialise from outer class

    public static var instance : DataService {
        get {
            if _instance == nil {
                DispatchQueue.global().sync(flags: .barrier) {
                    if _instance == nil {
                        _instance = DataService()
                    }
                }
            }
            return _instance!
        }
    }
}

-1
投票

使用静态变量和私有初始值设定项来创建单例类。

class MySingletonClass {

    static let sharedSingleton = MySingletonClass()

    private init() {}
}

-2
投票

这是我的实施。它还可以防止程序员创建新实例:

let TEST = Test()

class Test {

    private init() {
        // This is a private (!) constructor
    }
}

162
投票

对于Swift 1.2及更高版本:

class Singleton  {
   static let sharedInstance = Singleton()
}

有了正确性的证明(所有信用都是here),现在几乎没有理由使用任何以前的单身人士方法。

更新:现在这是official docs中描述的定义单身人士的官方方式!

至于使用static vs class的担忧。即使static变量可用,class也应该使用。单例并不意味着被子类化,因为这将导致基本单例的多个实例。使用static以美丽,Swifty的方式强制执行此操作。

对于Swift 1.0和1.1:

随着最近Swift的变化,主要是新的访问控制方法,我现在倾向于使用全局变量来实现单一的更简洁的方法。

private let _singletonInstance = SingletonClass()
class SingletonClass {
  class var sharedInstance: SingletonClass {
    return _singletonInstance
  }
}

正如Swift博客文章here中所提到的:

全局变量(也适用于结构和枚举的静态成员)的惰性初始化程序在第一次访问全局时运行,并作为dispatch_once启动,以确保初始化是原子的。这样就可以在代码中使用dispatch_once:只需使用初始化程序声明一个全局变量并将其标记为私有。

这种创建单例的方法是线程安全,快速,懒惰,并且还可以免费桥接到ObjC。


-2
投票
private var sharedURLCacheForRequestsKey:Void?
extension URLCache{
public static func sharedURLCacheForRequests()->URLCache{
    var cache = objc_getAssociatedObject(OperationQueue.main, &sharedURLCacheForRequestsKey)
    if cache is URLCache {

    }else{
        cache = URLCache(memoryCapacity: 0, diskCapacity: 1*1024*1024*1024, diskPath: "sharedURLCacheForRequestsKey")
        objc_setAssociatedObject(OperationQueue.main, &sharedURLCacheForRequestsKey, cache, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

    }
    return cache as! URLCache
}}

46
投票

Swift 1.2或更高版本现在支持类中的静态变量/常量。所以你可以使用一个静态常量:

class MySingleton {

    static let sharedMySingleton = MySingleton()

    private init() {
        // ...
    }
}

33
投票

有一种更好的方法。你可以在类的decleration之上声明一个全局变量,就像这样

var tpScopeManagerSharedInstance = TPScopeManager()

这只是在Swift中调用默认初始化或默认的init和全局变量dispatch_once。然后在您想要获得参考的任何课程中,您只需执行以下操作:

var refrence = tpScopeManagerSharedInstance
// or you can just access properties and call methods directly
tpScopeManagerSharedInstance.someMethod()

所以基本上你可以摆脱整个共享实例代码块。


27
投票

Swift单例在Cocoa框架中作为类函数公开,例如NSFileManager.defaultManager()NSNotificationCenter.defaultCenter(),所以我觉得作为镜像这种行为的类函数更有意义,而不是像其他一些解决方案那样使用的类变量,例如

class MyClass {

    private static let _sharedInstance = MyClass()

    class func sharedInstance() -> MyClass {
        return _sharedInstance
    }
}

通过MyClass.sharedInstance()检索单身人士。


16
投票

根据Apple documentation,它已被重复多次,在Swift中执行此操作的最简单方法是使用静态类型属性:

class Singleton {
    static let sharedInstance = Singleton()
}

但是,如果您正在寻找一种方法来执行除简单构造函数调用之外的其他设置,则秘诀是使用立即调用的闭包:

class Singleton {
    static let sharedInstance: Singleton = {
        let instance = Singleton()
        // setup code
        return instance
    }()
}

这保证是线程安全的,只能初始化一次。


16
投票

Swift 4+

protocol Singleton: class {
    static var sharedInstance: Self { get }
}

final class Kraken: Singleton {
    static let sharedInstance = Kraken()
    private init() {}
}

8
投票

看看Apple的示例代码,我遇到了这种模式。我不确定Swift如何处理静态,但这在C#中是线程安全的。我包括Objective-C互操作的属性和方法。

struct StaticRank {
    static let shared = RankMapping()
}

class func sharedInstance() -> RankMapping {
    return StaticRank.shared
}

class var shared:RankMapping {
    return StaticRank.shared
}
© www.soinside.com 2019 - 2024. All rights reserved.