问题
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
}
}
回答1:
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
orrequestedUpdateBudgetExhausted
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.
来源:https://stackoverflow.com/questions/34174337/complication-wont-update