添加DispatchQueue.main.async{}块来确保在主线程上更新UI。示例代码:
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) { print("getCurrentTimelineEntry") let now = Date() let entry = getTimelineEntry(for: complication, date: now) handler(entry) }
func getTimelineEntry(for complication: CLKComplication, date: Date) -> CLKComplicationTimelineEntry {
let template = complication.template
// Customize the template to display relevant information for the date.
template.textProvider = CLKSimpleTextProvider(text: "Hello World")
// Call the reloadTimeline(for:)
method for a complication on demand.
DispatchQueue.main.async { // Add this block to update UI
complication.reloadTimeline(for: complication)
}
return CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
}