
当从getNextRequestedUpdateDateWithHandler中设置的日期触发复杂的后台刷新时,我试图从手表本身获取数据.但是,当我调用HKHealthStore的执行方法时,如果应用程序(或在这种情况下,它)不会返回任何查询结果并发症)正在运行后台.我还尝试设置一个HKAnchoredobject查询,该查询应该在进程恢复时立即返回我的结果,但除非我在手表上手动启动应用程序扩展,否则这似乎也不会返回任何结果.这是我的监视代码,在请求运行状况工具包权限后从我的ExtensionDelegate的init方法调用:
func setupComplicationDataCache() { let Now = NSDate() var startDate: NSDate? = nil var interval: NSTimeInterval = 0 self.calendar.rangeOfUnit(NSCalendarUnit.Day,startDate: &startDate,interval: &interval,forDate: Now) let stepSampleType = HKQuantityType.quantityTypeForIDentifIEr(HKQuantityTypeIDentifIErStepCount)! // Match samples with a start date after the workout start let predicate = HKquery.predicateForSamplesWithStartDate(startDate,endDate: nil,options: .None) let query = HKAnchoredobjectquery(type: stepSampleType,predicate: predicate,anchor: nil,limit: 0) { (query,samples,deletedobjects,anchor,error) -> VoID in // Handle when the query first returns results self.handleStepResults(query,samples: samples,deletedobjects: deletedobjects,anchor: anchor,error: error) } query.updateHandler = { (query,error) -> VoID in // Handle update notifications after the query has initially run self.handleStepResults(query,error: error) } self.healthStore.executequery(query);}func handleStepResults(query: HKAnchoredobjectquery,samples: [HKSample]?,deletedobjects: [HKDeletedobject]?,anchor: HKqueryAnchor?,error: NSError?) { if error != nil { self.timelineModel.currentEntry = TimelineEntryModel(value: NSNumber(int: -1),endDate: NSDate()) } else if samples == nil || samples?.count == 0 { self.timelineModel.currentEntry = TimelineEntryModel(value: NSNumber(int: 0),endDate: NSDate()) } else { let newStepSamples = samples as! [HKQuantitySample] var stepCount = self.timelineModel.currentEntry.value.doubleValue var currentDate = self.timelineModel.currentEntry.endDate // Add the current entry to the collection of past entrIEs self.timelineModel.pastEntrIEs.append(self.timelineModel.currentEntry) // Add all new entrIEs to the collection of past entrIEs for result in newStepSamples { stepCount += result.quantity.doubleValueForUnit(self.countUnit) currentDate = result.endDate self.timelineModel.pastEntrIEs.append(TimelineEntryModel(value: NSNumber(double: stepCount),endDate: currentDate)) } // RetrIEve the latest sample as the current item self.timelineModel.currentEntry = self.timelineModel.pastEntrIEs.popLast() if self.timelineModel.currentEntry == nil { self.timelineModel.currentEntry = TimelineEntryModel(value: NSNumber(int: -3),endDate: NSDate()) } } // Reload the complication let complicationServer = CLKComplicationServer.sharedInstance() for complication in complicationServer.activeComplications { complicationServer.reloadTimelineForComplication(complication) }} 我也尝试使用HKObserverquery从iPhone获取数据.我有观察者查询,可以每小时唤醒一次iPhone(步骤数据的最大时间).但是,如果在观察者完成处理程序执行我的步骤查询时iPhone被锁定,则HKHealthStore的execute方法也拒绝返回任何查询结果.我认为这在这里是有道理的,并且可能没有办法解决这个问题,因为Apple’s docs提到当设备被锁定而你无法从商店读取时(仅写入),Health Store会被加密.但是在手表的情况下,当它在某人的手腕上没有锁定时,屏幕才会关闭.
有没有人知道如何在后台进行刷新时,无论是在iOS还是在watchOS 2上,都会让HealthKit更新出现在并发症上?
解决方法 经过大量测试后,我确定目前无法做到这一点.在watchOS 2上,当在后台运行扩展或并发症时,Apple似乎已完全禁用HealthKit查询返回结果.这包括从远程通知,Watch Connectivity以及复杂计划刷新执行.如果屏幕关闭且设备设置了密码,iPhone的HealthKit查询将失败.查询失败,因为在锁定设备时加密了运行状况数据存储.即使启用了观察者查询和后台传递,查询也会失败.您可以收到有关更改内容的通知,但在解锁iPhone之前无法查询更改(因为数据已加密).显示健康包相关数据的其他应用程序(例如步数和步行距离)通过直接查询计步器(CMpedometer)来实现,这些数据可在这些背景模式下访问.
对于没有在他们的设备上设置密码的iPhone用户来说,可能会产生一种复杂的背景更新,但这似乎是一个可怕的想法.
总结以上是内存溢出为你收集整理的ios – 如何在后台刷新的复杂功能上显示HealthKit数据?全部内容,希望文章能够帮你解决ios – 如何在后台刷新的复杂功能上显示HealthKit数据?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)