2017-10-19 8 views
0

Appleのdocsサンプルコードでは、addメソッドを使用して平均心拍数HKQuantitySampleをトレーニングに保存しますが、特定のトレーニングでは、トレーニング、つまり[HKQuantitySample]どうすればいいですか?以下は私のコードでテストするだけの最初の値を追加するものですが、それらをすべて追加したいのですか? ワークアウトにHKQuantitySamples(心拍数)の配列を保存する方法

private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) { 
    // Create energy and distance samples 
    let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(), 
                quantity: totalEnergyBurnedQuantity(), 
                start: startDate, 
                end: endDate) 

    let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(), 
               quantity: totalDistanceQuantity(), 
               start: startDate, 
               end: endDate) 

    let samples = [HKQuantitySample]() 
    samples.append(totalEnergyBurnedSample) 
    samples.append(totalDistanceSample) 
    samples.append(contentsOf: heartRateValues) 

    // Add samples to workout 
    healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in 
     guard success else { 
      print("Adding workout subsamples failed with error: \(String(describing: error))") 
      return 
     } 


     } 
    } 

は、基本的には、サンプルの配列を作成 totalEnergyBurnedSampletotalDistanceSample、その後、全体 heartRateValues配列を追加し healthStore.add方法でその sample引数を渡す:

var heartRateValues = [HKQuantitySample]() 

func processHeartRateSamples(_ samples: [HKQuantitySample]) { 
     for sample in samples { 
      heartRateValues.append(sample) 
     }  
    } 

private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) { 
     // Create energy and distance samples 
     let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(), 
                 quantity: totalEnergyBurnedQuantity(), 
                 start: startDate, 
                 end: endDate) 

     let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(), 
                quantity: totalDistanceQuantity(), 
                start: startDate, 
                end: endDate) 





     // Add samples to workout 
     healthStore.add([totalEnergyBurnedSample, totalDistanceSample, heartRateValues.first!], to: workout) { (success: Bool, error: Error?) in 
      guard success else { 
       print("Adding workout subsamples failed with error: \(String(describing: error))") 
       return 
      } 


      } 
     } 
+0

私は心拍数が記録されたときにあなたが欠けていると思います...あなたはそれを持っていますか? – Ladislav

+0

私はちょうど '日付'を使って得ることができますが、どのようにここに組み込まれるのでしょうか? – GarySabo

答えて

2

あなたはすでにこれだけやるheartRateValues[HKQuantitySample]として持っています。 ..

関連する問題