并发症不会更新

Complication won't update

我有一个非常简单的随机数并发症。 但是我的号码不会更新。每次我看手表都是一样的。仅当我重新安装复杂功能(重新安装 Apple Watch 应用程序)时,我才会收到一个新号码。

我已将更新设置为 1 秒。谁知道哪里出了问题?

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
    }
}

您的数据源丢失 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.

以下是您可以在计划的更新发生后重新加载时间线的方法:

// MARK: - Responding to Scheduled Updates

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

您还应该实施 requestedUpdateBudgetExhausted()

请记住,计划更新只能每 10 分钟发生一次;不可能每秒更新您的并发症。还要考虑更新过于频繁可能会耗尽您的更新预算:

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.