代码之家  ›  专栏  ›  技术社区  ›  lehn0058

使用HealthKit查询时,Apple watch复杂情况不会在后台刷新

  •  3
  • lehn0058  · 技术社区  · 9 年前

    我正试图在Apple Watch复杂程序中显示用户的每日步数。我通过调用HKHealthStore的requestAuthorizationToShareTypes方法来设置我的类,当第一次将其添加到手表表面时,复杂度会正确显示步骤。但是,在进行健康工具包查询时,刷新永远不会成功。我怀疑这与HealthKit权限有关,因为HKSampleQuery的完成处理程序没有被调用。如果我只是注释掉健康工具包查询,那么我的代码将按预期刷新。有人知道我可能缺什么吗?或者如果不允许复杂背景刷新访问HealthKit?

    下面是可以工作的代码块:

    /// 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) {
    
            let calendar = NSCalendar.currentCalendar()
            let now = NSDate()
            var startDate: NSDate? = nil
            var interval: NSTimeInterval = 0
            let endDate = NSDate()
    
            calendar.rangeOfUnit(NSCalendarUnit.Day, startDate: &startDate, interval: &interval, forDate: now)
    
            // Show dummy step data...
            let timelineEntry = self.buildTimelineEntry(complication, stepCount: 10, currentDateInterval: NSDate())
            handler(timelineEntry)
    }
    

    这是不起作用的代码块。错误情况下的更新甚至没有被调用:

    /// 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) {
    
            let calendar = NSCalendar.currentCalendar()
            let now = NSDate()
            var startDate: NSDate? = nil
            var interval: NSTimeInterval = 0
            let endDate = NSDate()
    
            calendar.rangeOfUnit(NSCalendarUnit.Day, startDate: &startDate, interval: &interval, forDate: now)
    
            let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.StrictStartDate)
            let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: true)
            let stepSampleType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
            let sampleQuery = HKSampleQuery(sampleType: stepSampleType, predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]) { (sampleQuery, results, error ) -> Void in
    
                if error != nil {
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        let timelineEntry = self.buildTimelineEntry(complication, stepCount: 10, currentDateInterval: NSDate())
                        handler(timelineEntry)
                    })
    
                    return
                }
    
                self.currentSteps = [HKQuantitySample]()
    
                if results != nil {
                    self.currentSteps = results as! [HKQuantitySample]
                }
    
                let countUnit = HKUnit(fromString: "count")
                var stepCount = 0.0
                var currentDate = now
                for result in self.currentSteps {
                    stepCount += result.quantity.doubleValueForUnit(countUnit)
                    currentDate = result.endDate
                }
    
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    let timelineEntry = self.buildTimelineEntry(complication, stepCount: stepCount, currentDateInterval: currentDate)
                    handler(timelineEntry)
                })
            }
    
            self.healthStore.executeQuery(sampleQuery)
    }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   user4151918 user4151918    9 年前

    尝试在复杂控制器内异步获取(HealthKit)数据是不可靠的。

    此外,尝试在复杂度控制器中获取或计算将不必要地占用分配给复杂度的执行时间预算。

    Apple recommends 在复杂数据源需要数据之前获取数据并将其缓存。

    数据源类的工作是尽快向ClockKit提供任何请求的数据。数据源方法的实现应该最少。不要使用数据源方法从网络获取数据、计算值或做任何可能延迟数据传输的事情。如果您需要获取或计算复杂度的数据,请在iOS应用程序或WatchKit扩展的其他部分中执行,并将数据缓存在复杂度数据源可以访问的位置。数据源方法应该做的唯一一件事就是获取缓存的数据并将其放入ClockKit所需的格式。

    推荐文章