-4

I have conformed to my WorkoutSessionManagerDelegate and have attempted to update the UI using those protocol methods but nothing in the update function is getting printed or displayed.

My setup requires initialization using context and I need to add code to update the WorkoutSessionManager when the context eventually changes.

How should I update the WorkoutSessionManager when the context eventually changes?

DashboardController.swift

class DashboardController: WKInterfaceController, WorkoutSessionManagerDelegate {

// IBOutlets

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    addMenuItemWithItemIcon(.Accept, title: "Save", action: #selector(DashboardController.saveSession))

    if context is WorkoutSessionContext {

   WorkoutSessionManager.sharedManager(context as! WorkoutSessionContext)

    } else {
        print("Context is not WorkoutSessionContext")
    }
}

func saveSession() {
    WorkoutSessionManager.sharedManager!.stopWorkoutAndSave()
}

func workoutSessionManager(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity) {

    // nothing in this function is getting printed or displayed
    caloriesLabel.setText("\((activeEnergyQuantity.doubleValueForUnit(workoutSessionManager.energyUnit)))")
    print("\(WorkoutSessionManager.sharedManager?.energyUnit)")
    print("testing print line")

}

WorkoutsController.swift

@IBAction func startWalkingButton() {
    print("Walking start button pressed")
    presentControllerWithName("Dashboard", context: sessionContext)
    WorkoutSessionManager.sharedManager!.startWorkout(.WalkingButton)   
// no code-completion
}
Edison
  • 11,881
  • 5
  • 42
  • 50
  • To create a true singleton the initialization must be private. This can also help you find illegal initializations in your code. – GlorySaber Aug 13 '16 at 20:59
  • Did you check that the function is actually being called? (With a breakpoint for example) – GlorySaber Aug 13 '16 at 21:11
  • No. I meant a break point to see if it was called at all. If your last print() call is not printing to console than it is not being called at all and the problem is not in the code you have shown here but instead in the code that calls the function. – GlorySaber Aug 13 '16 at 21:15
  • Show me the code from where you expect workoutSessionManager(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity) to be called from – GlorySaber Aug 13 '16 at 21:22
  • You must put a call to func workoutSessionManager(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity) where you want it to be called. Currently there is no call to that function. – GlorySaber Aug 13 '16 at 21:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120887/discussion-between-stephen-kac-and-tymac). – GlorySaber Aug 13 '16 at 21:45

1 Answers1

-1

You never called that function and to be able to call that function you must have a reference to DashboardController for you to use in your func startWalkingButton(). Since this is Watch Kit as we discussed above I do not know the best way to do this but Apples Sample Code here: https://developer.apple.com/library/ios/samplecode/WKInterfaceCatalog/Introduction/Intro.html will direct you on how to do that properly. If you need help with doing this ask the now more specific question you have.

GlorySaber
  • 367
  • 2
  • 12