How to Destroy a Singleton in Swift

How to destroy a singleton in Swift

Just a simple example on how to dispose the current instance of a Singleton:

import UIKit

class AnyTestClass
{
struct Static
{
private static var instance: AnyTestClass?
}

class var sharedInstance: AnyTestClass
{
if Static.instance == nil
{
Static.instance = AnyTestClass()
}

return Static.instance!
}

func dispose()
{
AnyTestClass.Static.instance = nil
print("Disposed Singleton instance")
}

func saySomething()
{
print("Hi")
}

}

// basic usage
AnyTestClass.sharedInstance.saySomething()
AnyTestClass.sharedInstance.dispose()

Hope it might help.

How to reset singleton instance?

I create all my Singletons with an optional Singleton instance.
However I also make this private and use a function to fetch it.
If the Singleton is nil it creates a new instance.

This is actually the only good way to set up a Singleton. If you have a regular object that you can't deinitialize it's a memory problem. Singletons are no different, except that you have to write a function to do it.

Singletons have to be completely self managed. This means from init to deinit.

I have a couple of templates on github for Singeltons, one of them with a fully implemented read/write lock.

class Singleton {

private static var privateShared : Singleton?

class func shared() -> Singleton { // change class to final to prevent override
guard let uwShared = privateShared else {
privateShared = Singleton()
return privateShared!
}
return uwShared
}

class func destroy() {
privateShared = nil
}

private init() {
print("init singleton")
}

deinit {
print("deinit singleton")
}
}

iOS: How can I destroy a Singleton in ARC? Should I?

If you destroy this singleton, you'll never be able to create it again (that's what the dispatch_once call means).

You don't need to destroy the singleton. By all means have a method on the singleton that removes any instance variables you no longer need, but there is no need to do anything else.

Recreate a singleton

You're on the right track. The key is to create a computed property that checks whether the status of the AVPlayer is .failed, and if so, recreates the instance. A second private static let contains a reference to your singleton and gets returned after the check:

import AVKit

class MyPlayer {
private static var avPlayer = AVPlayer()

private static let _sharedInstance = MyPlayer()

public static var sharedInstance: MyPlayer {
if self.avPlayer.status == .failed {
self.avPlayer = AVPlayer()
}

return self._sharedInstance
}

private init () {}
}

There is no need to recreate the singleton.

Destroy and Rebuild Singleton iOS

Why not put some logic in your singleton class to test if the connection to the device is active. If it has died, close the connection, and open a new one. This is effectively the same thing you are trying to do by destroying a recreating the singleton, but doesn't abuse the singleton pattern quite as much. It should also be simpler, because only the singleton knows about the connection, and thus keeps coupling low.

Ever need to destroy a singleton instance?

I wouldn't get hung up over the semantics of “singleton”—your requirement is that at most one instance of DBManager exist at any time. Once that instance is rendered useless, you can either destroy it so a fresh instance will be created on demand, or else define your newInstance method (which I might suggest be renamed getInstance) to throw an exception (perhaps IllegalStateException) if it is called after the singleton has been rendered useless.

If you are going to destroy it when rendered useless, I suggest that this be done inside the singleton class automatically, with no outside help. You also should consider completely hiding the singleton DBManager and implementing a delegation pattern instead. This will avoid the problem of a client keeping a reference to the stale DBManager instance. You can then make the delegate object a regular singleton.

Swift - How to set a singleton to nil

This should work:

private var _SingletonSharedInstance:MyClass! = MyClass()

class MyClass {
let prop = "test"

class var sharedInstance : MyClass {
return _SingletonSharedInstance
}

init () {}

func destroy() {
_SingletonSharedInstance = nil
}
}

But then the references to the object are still kept, so you need to do some additional things to invalidate method calls in the class.



Related Topics



Leave a reply



Submit