CLKComplicationDataSource:getTimelineEntries委托调用数百次

时间:2017-02-09 23:49:26

标签: ios watchkit apple-watch-complication time-travel

我正在尝试在Watch app Complication中支持TimeTravel。 考虑一下这段代码。

我无法理解为什么会重复调用这些委托方法,直到我杀死该应用程序。

我想知道我做错了什么,因为操作系统负责调用这些方法。这是某种WatchOS错误吗?

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

来自控制台日志的小摘录:

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    

此时,我杀了应用程序......

1 个答案:

答案 0 :(得分:0)

事实证明,代表被调用了太多次,因为我会继续提供相同日期的时间表条目。

我的代码中有一个错误(此处未显示),负责创建CLKComplicationTimelineEntry对象。我没有使用委托提供的日期,而是使用Date()调用(显然是从getCurrentTimelineEntry方法的实现中复制粘贴的。)

我发现令人印象深刻的是WatchOS会将代表称为数千次