Apple Watch Complication在后台未更新

lehn0058

我有一个Apple Watch并发症,它可以正确初始化并显示我期望的数据。但是,当我的getNextRequestedUpdateDateWithHandler方法中返回的NSDate触发刷新时,委托中唯一被再次调用的方法就是getNextRequestedUpdateDateWithHandler方法(我在每个方法上都设置了断点来确定此方法)。我希望在发生请求的更新日期时调用requestUpdateDidBegin,但是事实并非如此。有谁知道是什么原因造成的?这是我的代码:

class ComplicationController: NSObject, CLKComplicationDataSource {

/// Provide the time travel directions your complication supports (forward, backward, both, or none).
func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
    handler(.Backward)
}

/// Depending on which time travel directions you support, you will be asked for the start/end dates of your timeline (or both, or neither).
/// The start/end dates will determine at what point during time travel we dim out your data to indicate that the timeline does not continue in this direction.
/// Timeline entries after the timeline end date or before the timeline start date will not be displayed.
func getTimelineStartDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
    let calendar = NSCalendar.currentCalendar()
    let now = NSDate()
    var startDate: NSDate? = nil
    var interval: NSTimeInterval = 0

    calendar.rangeOfUnit(NSCalendarUnit.WeekOfMonth, startDate: &startDate, interval: &interval, forDate: now)
    handler(startDate)
}

func getTimelineEndDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
    handler(NSDate())
}

/// Indicate whether your complication's data should be hidden when the watch is locked.
func getPrivacyBehaviorForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationPrivacyBehavior) -> Void) {
    // Since this is showing health data, we want to secure this when the device is locked.
    handler(.HideOnLockScreen)
}

/// Indicate your complication's animation behavior when transitioning between timeline entries.
func getTimelineAnimationBehaviorForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineAnimationBehavior) -> Void) {
    handler(.Always)
}

/// Provide the entry that should currently be displayed.
/// If you pass back nil, we will conclude you have no content loaded and will stop talking to you until you next call -reloadTimelineForComplication:.
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {
    // ... custom entry code
    handler(currentTemplate)
}

/// The owning complication will use these methods to extend its timeline backwards or forwards.
/// @param date The date of the first/last entry we already have. Return the batch of entries before/after this date.
/// @param limit Maximum number of entries to return.
func getTimelineEntriesForComplication(complication: CLKComplication, beforeDate date: NSDate, limit: Int, withHandler handler: ([CLKComplicationTimelineEntry]?) -> Void) {

    //... custom entry code
    handler(templates)
}

func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: ([CLKComplicationTimelineEntry]?) -> Void) {

    handler([CLKComplicationTimelineEntry]())
}

/// Return the date when you would next like to be given the opportunity to update your complication content.
/// We will make an effort to launch you at or around that date, subject to power and budget limitations.
func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Refresh in 30 minutes
    let refreshDate = NSDate().dateByAddingTimeInterval(60*30)
    handler(refreshDate)
}

/// This method will be called when you are woken due to a requested update. If your complication data has changed you can
/// then call -reloadTimelineForComplication: or -extendTimelineForComplication: to trigger an update.
func requestedUpdateDidBegin() {
    let complicationServer = CLKComplicationServer.sharedInstance()
    for complication in complicationServer.activeComplications {
        complicationServer.reloadTimelineForComplication(complication)
    }
}

/// This method will be called when we would normally wake you for a requested update but you are out of budget. You can can
/// trigger one more update at this point (by calling -reloadTimelineForComplication: or -extendTimelineForComplication:) but
/// this will be the last time you will be woken until your budget is replenished.
func requestedUpdateBudgetExhausted() {
    let complicationServer = CLKComplicationServer.sharedInstance()
    for complication in complicationServer.activeComplications {
        complicationServer.reloadTimelineForComplication(complication)
    }
}

/// When your extension is installed, this method will be called once per supported complication, and the results will be cached.
/// If you pass back nil, we will use the default placeholder template (which is a combination of your icon and app name).
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
    //... custom template code

    handler(template)
}
}
用户4151918

endDate是您的并发症数据源准备提供数据的最晚日期。如果结束日期已过,则意味着两件事:

  • 由于已到达时间轴的末尾,因此最新的时间轴条目将变暗。

  • 复杂服务器将意识到,要求您的endDate之后的条目是毫无意义的,因为将来可能不会存在任何条目。在这种情况下,如您所注意到的,并发症服务器将仅要求新的更新日期。

并发症服务器使用72小时的滑动窗口,以确保24小时的时间在任一方向上行驶。

即使您不支持提前旅行,也应endDate出于以下几个原因确保将来

  • 如果您不希望当前条目变灰,则在重新加载时间线之前一定不能到达结束日期。

  • 由于时间轴仍然是最新的,因此并发症服务器必须知道可以要求您的数据源提供其他条目。

每次重新加载时间轴时,都会要求一个新的开始和结束日期。您的结束日期不必在遥远的将来,因为它最多每30分钟更新一次,但它仍应该是将来的一天,以防由于频繁的更新而使您的每日预算用尽。

顺便说一句,复杂服务器确实考虑了时间旅行边界日期,以确定是否应将条目添加到时间线。

earliestTimeTravelDate

构建时间轴时,请勿在此日期之前创建任何条目。这样做会浪费时间,因为这些条目将永远不会显示。

如果您要提供可追溯到一周开始的条目,则可能要避免在时间旅行范围之外创建条目,以免耗尽您的预算。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章