cojoj
cojoj

Reputation: 6475

Querying HealthKit's correlations, but using anchor

My app is basically gathering a lot o info from HealthKit and later it uses mathematical models to analyze data and provide us with feedback.
One of many samples we're interested in is blood pressure, but HealthKit is treating this as HKCorrelationTypeIdentifierBloodPressure - a set of related HKSamples which, in this case are, HKQuantityTypeIdentifierBloodPressureDiastolic and HKQuantityTypeIdentifierBloodPressureSystolic.

We're gathering all samples and for this I'm using HKAnchoredObjectQuery but this one accepts only one HKSampleType. Pn the other hand, if I use HKCorrelationQuery I have to specify dates range for this query which doesn't satisfy my requirements... And here I am, stuck with no good solution.


The simplest solution, maybe not the prettiest one, is to create a dispatch_group and as separately for diastolic and systolic samples. When I get them, try to merge two arrays based on startDate and endDate... Phew! From what I can see in Health.app, user is able to enter correlation at once, so this should be doable based on dates.

Another one that just came into my mind... Maybe it's possible to create HKAnchoredObjectQuery and pass HKCorrelationType as parameter?

Upvotes: 1

Views: 817

Answers (1)

cojoj
cojoj

Reputation: 6475

Thanks to @Allan for some hints I was able to do this using HKAnchoredObjectQuery. Everything's because HKCorrelationType is a subclass of HKObjectType, so you can actually pass any correlation to every general purpose method in HealthKit which requires HKObjectType.

There was one additional concern about this solution - fact that HKAnchoredQuery returns [HKSample]? but we want to determine systolic and diastolic but this turned out to be solved by HealthKit as well... In return we get [HKCorrelation]? and each element has property objects, which is Set<HKSample>, so we don't have to worry about getting everything mixed up and we can easily cast the results to any method accepting HKSample by simply checking sampleType property.

Upvotes: 3

Related Questions