2016-03-25 22 views
-2

心拍数を取得するためにこのコードを書いています。NSArrayを使用して心拍数を平均しています。 Apple WatchはHealth Kitのデータを更新しています。更新された平均心拍数が1分ごとに更新されるようにします。 私はこの時点で立ち往生しています、助けてください?ヘルスキットから心拍数データを更新する

-(double)get_heartRates 
{ 

//code to get the updated heart beats 
NSDate *startDate1 = [NSDate distantPast]; 
NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:startDate1 endDate:[NSDate date] options:HKQueryOptionStrictEndDate]; 
HKSampleType *object = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; 

sum_Of_HeartRates=0.0; 

HKAnchoredObjectQuery *heartQuery = [[HKAnchoredObjectQuery alloc] initWithType:object predicate:Predicate anchor:self.lastAnchor limit:0 resultsHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *sampleObjects, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *newAnchor, NSError *error) { 

    NSLog(@"Sample counts:%ld",sampleObjects.count); 
    for(int i=0;i<(int)sampleObjects.count;i++) 
    { 

     HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects objectAtIndex:i]; 
     HKQuantity *quantity = sample.quantity; 
     double bpm_Values= [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]; 
     sum_Of_HeartRates=sum_Of_HeartRates+bpm_Values; 

    } 
    avg_heartBeats=sum_Of_HeartRates/(int)sampleObjects.count; 
}]; 

[heartQuery setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *SampleArray, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *Anchor, NSError *error) { 

    HKQuantitySample *sample = (HKQuantitySample *)[SampleArray objectAtIndex:0]; 
    HKQuantity *quantity = sample.quantity; 
    new_Updated_Data =[quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]; 
    NSLog(@"new quantity:%f",new_Updated_Data); 
}]; 

[self.healthStore executeQuery:heartQuery]; 
NSLog(@"updated data %f",new_Updated_Data); 


return avg_heartBeats; 


//todo:- to get background update fast and easy 

}

+0

誰も教えてもらえますか? –

+0

あなたはiPhoneまたは時計でこのconを実行しますか? –

+0

iphone、ヘルスキットからデータを取得しています –

答えて

0

使用HKObserverQueryはバックグラウンドで更新された長時間実行クエリを取得するには:

https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKObserverQuery_Class/index.html

限りなしHKWorkoutSessionとして実行されている、あなたの時計の対策心拍ごと10分なので、値を増やせません。時計でHKWorkoutSessionが実行されていると、値がより頻繁に取得されます。

あなたはHKWorkoutSessionに飛び込むしたい場合は、アランは、詳細なチュートリアルがあります:私は与えられていますなぜ-2この質問にhttps://developer.apple.com/videos/play/wwdc2015/203/

+0

HKAnchoredQueryの問題は何ですか?私は更新ハンドラを使用していましたか? WorkoutSessionなしで同じものを使うことはできますか? –

+0

'HKAnchoredQuery'では問題ありません。 'HKWorkoutSession'は' HKWorkoutSession'なしで使うことができます( 'HKWorkoutSession'は時計でしか利用できません)。 –

+0

こんにちはゲルト今、私はこの問題に直面しています。私は更新されたデータを取得していますが、マルチタスキングする必要があり、マルチタスキングやHealth Kitに触れることなく適切なライブデータを取得します。 36279850?noredirect = 1#comment60186455_36279850これで助けてください? –

関連する問題