Paul John
Paul John

Reputation: 115

Is there a reason my singleton instantiates only when referenced? How can I make it instantiate when my app launches?

class TheOneAndOnlyKraken {
    static let sharedInstance = TheOneAndOnlyKraken()
}

This is the blue print of the singleton I implemented. I use my singleton in order to get the user's location throughout all of the project files, and the issue is that in the first time I get that information is returns nil (because it needs a second to setup I guess). This is why I'm interested in instantiating the singleton as soon as the app launches, to avoid working with nil location.

Upvotes: 1

Views: 44

Answers (1)

Yun CHEN
Yun CHEN

Reputation: 6648

Why not call your singleton in AppDelegate:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        _ = TheOneAndOnlyKraken.sharedInstance
        return true
   }

Upvotes: 3

Related Questions