2015-10-05 35 views
1

我試圖在創建HKObserverQuery後觸發enableBackgroundDeliveryForType,但我意識到即使方法本身被禁用。Healthkit enableBackgroundDeliveryForType不可用,無法計算每小時的心率

/*! 
@method  enableBackgroundDeliveryForType:frequency:withCompletion: 
@abstract  This method enables activation of your app when data of the type is recorded at the cadence specified. 
@discussion When an app has subscribed to a certain data type it will get activated at the cadence that is specified 
       with the frequency parameter. The app is still responsible for creating an HKObserverQuery to know which 
       data types have been updated and the corresponding fetch queries. Note that certain data types (such as 
       HKQuantityTypeIdentifierStepCount) have a minimum frequency of HKUpdateFrequencyHourly. This is enforced 
       transparently to the caller. 
*/ 

那麼如何觸發一個工作,每小時檢查心率。

healthStore.enableBackgroundDeliveryForType(sampleType, frequency: .Immediate, withCompletion: {(succeeded: Bool, error: NSError?) in 

     if succeeded{ 
      print("Enabled background delivery of weight changes") 
     } else { 
      if let theError = error{ 
       print("Failed to enable background delivery of weight changes. ") 
       print("Error = \(theError)") 
      } 
     } 
    }) 

以下是我正在運行的查詢以獲取示例。

 let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)! 

     //let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) 



     let query = HKObserverQuery(sampleType: sampleType, predicate: nil) { 
      query, completionHandler, error in 

      if error != nil { 

       // Perform Proper Error Handling Here... 
       print("*** An error occured while setting up the stepCount observer. ***") 
       abort() 
      }else{ 
       print("sampleType ",sampleType) 
      } 
      self.heightChangedHandler() 
      completionHandler() 
     } 

func heightChangedHandler(){ 
    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, 
       ascending: true) 

      let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)! 
      let query = HKSampleQuery(sampleType: sampleType, predicate: nil, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { (query: HKSampleQuery, results: [HKSample]?, error: NSError?) -> Void in 

       guard let results = results where results.count > 0 else { 
        print("Could not read the user's weight") 
        print("or no weight data was available") 
        return 
       } 

       for sample in results as! [HKQuantitySample]{ 

        let heartRate = sample.quantity 
        let heartRateDouble = heartRate.doubleValueForUnit(self.countPerMinuteUnit) 
        print("Sample ",sample.quantity ," tyoe ",sample.quantityType," heartRateDouble ",heartRateDouble) 

       } 

      } 

      healthStore.executeQuery(query) 
} 

回答

3

無法安排watchOS 2.0應用程序定期在後臺啓動以查詢HealthKit數據。 watchOS 2.0的應用程序通常只能在手錶屏幕開啓的情況下在前臺運行。

2

2件事情,您嘗試訪問的一種方法目前僅適用於iPhone。 Apple手錶甚至沒有背景狀態,只是處於非活動狀態而已。第二個蘋果承諾,觀看應用程序擴展,而運行訓練時,將允許留在前臺,這通常是真實的,直到用戶的手腕下降:(無論如何,祝你好運,希望你能想出如何實現你的目的..

我我對這個過程並不熟悉,但是你可以安排一個複雜功能來每小時更新一次,你可以嘗試從健康商店中提取最後一個已知的心率對象。無論如何,從來沒有爲我的目的嘗試過它,並且幾乎可以肯定有一些事情會需要改變,但只是一個想法

或我剛纔想到的實施的東西..嘗試捕捉手機店的變化,然後通過後臺傳輸發送數據到手錶。基本上,你可以使立即更新或每天..然後將它從手機發送到手錶,對於我希望的後臺應用程序來說不會是一件壞事。