1

In my controller, I have a button that reloads the controller with the following code, to hide the back button:

WKInterfaceController.reloadRootPageControllers(withNames: ["Record"], contexts: [], orientation: .vertical, pageIndex: 0)

When this code is triggered the awake function is called again. My awake function is as follows:

override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    let track = context as! Track

    locationManager.trackMetaData = track

    //create new session
    sessionManager.createNewSession(track: locationManager.trackMetaData!)

    finishButton.setHidden(true)
}

The function sessionManager.createNewSession can only be called once, and the parameter required for this function requires the context parameter. How can I make sure this code is only called once? I could set a boolean variable in the init() function, and then check it hasn't already been executed int his one, but is that the right way to go about it?

toast
  • 1,860
  • 2
  • 26
  • 49
  • just check to see if trackMetaData or sessionManager has already been defined - if it has, then return – Russell Jun 03 '18 at 08:09
  • Check if the class for sessionManager has some state property you can use for this or as you suggested yourself create and manage your own state property for the session – Joakim Danielson Jun 03 '18 at 10:11

0 Answers0