1

I need to create statistics for month of HKQuantityType with Cumulative aggregation style, like in Health App on year view.

HKStatisticsCollectionQuery can calculate only sum for Cumulative types.

How it works now. I'm making 12 HKSampleQueries. Each quest has predicate with start and end dates of a month. Then I'm iterating over each query result to calculate number of days that has samples (One day can contain more then one sample). After number of days ready I'm divide sum of moths on number of days.

Main problem is that if I get all samples in a moth, I got about 10000 samples. Number of month is 12 so it's about 120000 samples for the one Metric in a year.

I'm running it in a background threads but amount of memory is about 180mb while calculating average for each month and it takes about a minute to calculate.

Is there any possibilities to get number of samples in a month, groped by day? I didn't found any solution for it :(

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
artyom
  • 48
  • 11
  • Can you explain in more detail what you are trying to calculate? I'm having trouble imagining a reason you would want to find the average of a collection of cumulative samples. – Allan Jun 22 '16 at 18:06
  • I want to calculate average value for month for Cumulative values. For that I need to know number of samples for the month. Then I can divide sum of month by number of samples in month. Main problem is that we could have several samples in a day. I tried to get all samples for year in a one query and loop through array. But it's really have operation because we could have over 3000 samples :( – artyom Jun 24 '16 at 06:19
  • @Allan does it clear explanation? – artyom Jul 02 '16 at 13:10
  • Do you want the average value of each cumulative sample? That wouldn't make much sense to me, since each sample can cover wildly different amounts of time (some might be the total for 1 minute and some 1 hour). Or do you want to calculate the average sum per fixed amount of time, like an average of the daily totals for a month? It might help if you were more specific about which data type and what information you are trying to convey to the user. – Allan Jul 03 '16 at 19:09
  • I need average value of each HKQuantityType with Cumulative aggregation style in HealthKit. For example I need average value of HKQuantityTypeIdentifierBasalEnergyBurned for each month in last year. So it will start at Jul 2015, Aug 2015 ... Jul 2016. Then the same for HKQuantityTypeIdentifierStepCount etc. – artyom Jul 04 '16 at 06:18
  • The same as for HealthApp on Year tab for charts (Dashboard) – artyom Jul 04 '16 at 07:23
  • Ok got it. Calculating that average doesn't involve the count of samples per month, though. I'll explain further in an answer. – Allan Jul 04 '16 at 14:33

1 Answers1

2

Try HKStatisticsCollectionQuery with an interval of one day. You can then average the sums per day that are returned. It should be orders of magnitude more efficient than querying for all the samples in each month (and more correct since you would need to account for overlapping samples returned by HKSampleQuery).

Allan
  • 7,039
  • 1
  • 16
  • 26
  • Thank you! It works match faster then with SampleQuery. Stat for the 12 months now completed in 5-7 seconds. I didn't though that it's really fast solution :) – artyom Jul 05 '16 at 07:52