gpt4 book ai didi

watchos-2 - 使用 HealthKit 查询时,Apple Watch 并发症不会在后台刷新

转载 作者:行者123 更新时间:2023-12-04 14:30:53 31 4
gpt4 key购买 nike

我正在尝试在 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)
}

最佳答案

尝试在并发症 Controller 中异步获取 (HealthKit) 数据是不可靠的。

此外,尝试在并发症 Controller 内获取或计算将不必要地用完分配给您的并发症的执行时间预算。

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

The job of your data source class is to provide ClockKit with any requested data as quickly as possible. The implementations of your data source methods should be minimal. Do not use your data source methods to fetch data from the network, compute values, or do anything that might delay the delivery of that data. If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension, and cache the data in a place where your complication data source can access it. The only thing your data source methods should do is take the cached data and put it into the format that ClockKit requires.

关于watchos-2 - 使用 HealthKit 查询时,Apple Watch 并发症不会在后台刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569001/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com