4

I have issues getting Complications to work. It would be helpful if I was able to reliably refresh them.

Therefore I linked a force-press menu button to the following method

@IBAction func updateComplication() {
    let complicationServer = CLKComplicationServer.sharedInstance()
    for complication in complicationServer.activeComplications {
        complicationServer.reloadTimelineForComplication(complication)
    }        
}

Unfortunately this leads to the app crashing. with a fatal error: unexpectedly found nil while unwrapping an Optional value.

I understand that calling reloadTimelineForComplication(complication) is budgeted but that can't be the issue here as it doesn't work from the very beginning.

I am currently using watchOS2 + Xcode 7 GM

I'd appreciate any ideas on making Complications refresh while the app is running?

Bernd
  • 11,133
  • 11
  • 65
  • 98

1 Answers1

7

Trace or use the exception breakpoint and focus on reading the whole error message where it tells you exactly on which line it found the nil unexpectedly (I do suspect the complicationServer). Use 'if let' instead of 'let' to force unwrap the respective variable.

private func reloadComplications() {        
    if let complications: [CLKComplication] = CLKComplicationServer.sharedInstance().activeComplications {
        if complications.count > 0 {
            for complication in complications {
                CLKComplicationServer.sharedInstance().reloadTimelineForComplication(complication)
                NSLog("Reloading complication \(complication.description)...")
            }
            WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Click) // haptic only for debugging
        }
    }
}
igraczech
  • 2,408
  • 3
  • 25
  • 30
  • 1
    One thing: Do not forget to add observer for CLKComplicationServerActiveComplicationsDidChangeNotification to update it continuously – AlexeyVMP Apr 01 '16 at 06:24
  • 1
    It fires on events like watch face change – AlexeyVMP Apr 03 '16 at 06:39
  • why CLKComplicationServer.sharedInstance().activeComplications always returns nil? – Mahdi Moqadasi Mar 17 '23 at 16:57
  • Is it always? I think it is only when throttling disables that complications... that's why there is the `if let` construct, because this happens most of the time (except for when the complications are scheduled for redraw). – igraczech Mar 22 '23 at 22:43