问题描述:

I am trying to support TimeTravel in a Watch app Complication.

Consider this code.

I cannot understand why these delegate methods are called repeatedly, until I kill the app.

I wonder what I could be doing wrong, since the OS is responsible for calling these methods. Is this some kind of WatchOS bug?

func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([ CLKComplicationTimelineEntry]?) -> Void) {

// Call the handler with the timeline entries after to the given date

let interval = stride(from:0.0, through:60, by: 10.0)

var entries: [CLKComplicationTimelineEntry]? = nil

switch complication.family {

case .circularSmall:

entries = interval.flatMap({ minutes in

entryForCircularSmall(date: date.addingTimeInterval(60.0 * minutes) )

})

handler(entries)

case .utilitarianLarge:

entries = interval.flatMap({ minutes in

entryForUtilitarianLarge( date: date.addingTimeInterval(60.0 * minutes) )

})

handler(entries)

default:

handler(nil)

}

print("future timelineEntries limit: \(limit), date: \(date), entries: \(entries?.count)")

}

func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

let date = Date().addingTimeInterval(60.0 * Double(240))

print("timeline end: \(date)")

handler(date)

}

Small excerpt from the console log:

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:32:59 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:09 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:09 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

future timelineEntries limit: 100, date: 2017-02-09 23:33:10 +0000, entries: Optional(7)

timeline end: 2017-02-10 03:33:10 +0000

At this point, I kill the app...

网友答案:

As it turns out, the delegate was called too many times, because I would keep supplying timeline entries for the same date.

I had a bug in my code (not shown here) responsible for creating the CLKComplicationTimelineEntry objects. Instead of using the date supplied by the delegate, I was using a call to Date() (apparently copy-pasted from the implementation of the getCurrentTimelineEntry method).

I find impressive however that WatchOS would call the delegate thousands of times.

相关阅读:
Top