Watchos 2 Apple Watch未在后台更新

Watchos 2 Apple Watch未在后台更新,watchos-2,apple-watch-complication,clockkit,Watchos 2,Apple Watch Complication,Clockkit,我有一个Apple Watch复杂功能,可以正确初始化并显示我期望的数据。但是,当GetNextRequestedUpdateWithHandler方法中返回的NSDate触发刷新时,委托中唯一再次被调用的方法是GetNextRequestedUpdateWithHandler方法(我在每个方法上设置断点以确定这一点)。我本以为当请求的更新日期出现时RequestedUpdatedDidBegin会被调用,但事实似乎并非如此。有人知道这是什么原因吗?这是我的密码: class Complicat

我有一个Apple Watch复杂功能,可以正确初始化并显示我期望的数据。但是,当GetNextRequestedUpdateWithHandler方法中返回的NSDate触发刷新时,委托中唯一再次被调用的方法是GetNextRequestedUpdateWithHandler方法(我在每个方法上设置断点以确定这一点)。我本以为当请求的更新日期出现时RequestedUpdatedDidBegin会被调用,但事实似乎并非如此。有人知道这是什么原因吗?这是我的密码:

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

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

  • 当到达时间线的末尾时,最新的时间线条目将变暗

  • 服务器会意识到,请求超过endDate的条目是没有意义的,因为将来可能不存在任何条目。在这种情况下,服务器将只要求一个新的更新日期,正如您所注意到的

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

尽管您不支持向前时间旅行,但出于以下几个原因,您应该确保未来的
endDate

  • 如果您从不希望当前条目变暗,则在重新加载时间线之前不得到达结束日期

  • 服务器必须知道,由于您的时间线仍然是最新的,因此可能会要求您的数据源提供其他条目

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

另外,不考虑时间旅行边界日期,以确定是否应将条目添加到时间线中

earliestTimeTravelDate

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


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

是否有理由使用过去的时间线结束日期?我相信系统不会故意要求你输入新的条目,因为它们将超过你的截止日期。我有点怀疑这种方法。从苹果的文档中,我认为这代表了你有信息的最后一个日期,因此时间旅行将在该日期之后淡出你的复杂性(我的复杂性只有当前和过去的数据)。如果我打算每隔30分钟就有一次更新数据,我会不会希望这个日期在遥远的未来某个时候?谢谢你的详细回答——苹果应该考虑用你的文档替换他们的描述:)我不能理解苹果的时间线概念,我不能让它正常工作。当更新复杂度很有效时,他们可以轻松调用委托函数,为什么他们会让它变得如此复杂?我们知道每个人都想尽快更新他们的并发症!