Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do I need to keep the reference to a singleton object to avoid repeated initialisation?

Imagine that we have some singleton object:

class Singleton {
    static var shared = Singleton()

    private init() { ... }
}

Am I right that if I don’t keep the reference in some place, it is initialised again and again due to the ARC every time I access it, like this:

Singleton.shared.doSomething()
var a = Singleton.shared.returnSomething()

If I am, where to keep the reference in the iOS app? In classes that use the singleton?
Or in AppDelegate, to ensure using the same instance without repeated initialisation?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By assigning it to a static value you retain the shared instance and don’t need to reinitialise it. Static values exist at class level, not instance level, so are retained, effectively, indefinitely.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading