How to reset singleton instance?

I am creating a singleton instance like this

static let currentUser = User() private override init() { super.init() // custom initialisation } 

How can I reset to create this instance or set to zero?

+5
source share
3 answers

I create all my singleton with an optional Singleton instance. However, I also do this private and use a function to retrieve it. If Singleton is nil , it creates a new instance.

This is actually the only good way to configure Singleton. If you have a regular object, you cannot deinitialize its memory problem. Singletones are no different, except that you need to write a function to do this.

Singleton must be fully controlled. This means from init to deinit.

I have several github templates 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") } } 
+18
source

You cannot do this if you declare currentUser as let . It should be var instead, or better still private (set) var . Also, you cannot assign currentUser using nil if its type is User (inferred from the way you currently assigned it). Instead, should it be User? .

For example, something like this:

 /// ... static private (set) var currentUser: User? = User() static func resetCurrentUser() { currentUser = nil } // ... 

private (set) allows you to change the properties only within the current file, and for the rest of the code it will look like let . Then the resetCurrentUser() method can be used to put it in nil .


Or even this:

 // ... private static var _currentUser: User? static var currentUser: User { if _currentUser == nil { _currentUser = User() } return _currentUser! } static func resetCurrentUser() { _currentUser = nil } // ... 

You can have currentUser as a computed property that guarantees a return value. That way you can reset user nil , yes. But if you later try to read from there, a new instance will be created automatically.

Be careful with multi-threaded access.

+5
source

All that you need is possible, but highly discouraged :) Because single-player games by design should not return to zero.

First, if you want to change currentUser , it should be var . Then, if you want it to be zero, it must be of an optional type, and you must expand it when used.

 static var currentUser: User? = User() 

I would suggest not changing currentUser or making it non-static (for example, a property of some UsersManager .

You can also change the currentUser properties (e.g. name , loggedIn ). Finally, take a look at this answer: fooobar.com/questions/1237264 / ... - it describes your situation.

+1
source

Source: https://habr.com/ru/post/1237261/


All Articles