3

I have a very simple complication with a random number. But my number won't update. Everytime I look on my watch it's the same. Only if I reinstall the complication (reinstalling apple watch app) I'm getting a new number.

I have set update to 1 second. Anyone an idea what could be wrong?

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
    handler(CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: getTemplateForComplication(family: complication.family)!))
}

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    handler(NSDate(timeIntervalSinceNow: 1))
}

func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
    handler(getTemplateForComplication(family: complication.family))
}

func getTemplateForComplication(family family: CLKComplicationFamily) -> CLKComplicationTemplate? {
    let bitcoinPrice = Double(arc4random_uniform(400))
    switch family {
    case .ModularSmall:
        let template = CLKComplicationTemplateModularSmallSimpleText()
        template.textProvider = CLKSimpleTextProvider(text: String(format: "%.2f", bitcoinPrice))
        return template
    case .ModularLarge:
        let template = CLKComplicationTemplateModularLargeTallBody()
        template.headerTextProvider = CLKSimpleTextProvider(text: "Bitcoin")
        template.bodyTextProvider = CLKSimpleTextProvider(text: String(format: "%.2f €", bitcoinPrice))
        return template
    case .UtilitarianSmall:
        let template = CLKComplicationTemplateUtilitarianSmallFlat()
        template.textProvider = CLKSimpleTextProvider(text: String(format: "%.2f", bitcoinPrice))
        return template
    case .UtilitarianLarge:
        let template = CLKComplicationTemplateUtilitarianLargeFlat()
        template.textProvider = CLKSimpleTextProvider(text: String(format: " %.2f €", bitcoinPrice))
        return template
    default:
        return nil
    }
}
Wed
  • 553
  • 5
  • 20
  • do you write, that every time you call getTemplateForComplication your bitcoinPrice is the same?? – user3441734 Dec 09 '15 at 08:59
  • every time I look on my watch it's the same price. – Wed Dec 09 '15 at 09:00
  • i don't know to much about watch-os, but the line where you generate bitcoinPrice seems fine to me and for sure it generates something else on each call of getTemplateForComplication. you have to have trouble somewhere else in you code, not with generation of random double value – user3441734 Dec 09 '15 at 09:07
  • after a reinstallation of the complication (watchOS app) I'm getting a new number. But it won't update in the background. – Wed Dec 09 '15 at 09:08
  • you are better to redefine you question, to be sure somebody can help you. – user3441734 Dec 09 '15 at 09:16
  • redefine in to? I could add some more information – Wed Dec 09 '15 at 09:19
  • So I checked the documentation of `getPlaceholderTemplateForComplication:withHandler:` and it says "Returns a static template...", "The contents of the placeholder are not updated...", does it relate to your question ?\ – A-Live Dec 09 '15 at 09:29
  • That's just the placeholder while choosing a complication. getCurrentTimelineEntryForComplication should update it, as far as I understood. – Wed Dec 09 '15 at 09:31

1 Answers1

3

Your data source is missing the methods that are responsible for handling the scheduled update.

At the start of a scheduled update, ClockKit calls either the requestedUpdateDidBegin or requestedUpdateBudgetExhausted method, depending on the state of your complication’s time budget. You must implement one or both of those methods if you want to add data to your timeline. Your implementation of those methods should extend or reload the timeline of your complication as needed. When you do that, ClockKit requests the new timeline entries from your data source. If you do not extend or reload your timeline, ClockKit does not ask for any new timeline entries.

Here's how you can reload the timeline once your scheduled update occurs:

// MARK: - Responding to Scheduled Updates

func requestedUpdateDidBegin() {
    let server=CLKComplicationServer.sharedInstance()
    for complication in server.activeComplications {
        server.reloadTimelineForComplication(complication)
    }
}

You should also implement requestedUpdateBudgetExhausted().

Keep in mind that scheduled updates can only occur every 10 minutes; it's not possible to update your complication every second. Also consider that updating too often might exhaust your update budget:

Specify a date as far into the future as you can manage. Do not ask the system to update your complication within minutes. Instead, provide data to last for many hours or for an entire day. If your budget is exhausted, the next scheduled update does not occur until after your budget is replenished.

  • working great, thank you. 1 second was just for testing purposes. I think I will set it to an hour or something like that. – Wed Dec 15 '15 at 08:45