HKStatisticsCollectionQuery pulling data for each week of the month

I am trying to create a chart that displays monthly data versus last month's data broken down by week.

For example, viewing the chart today shows July versus June, each chart will have 5 points with the following dates:

June: 1-8, 9-15, 16-22, 23-29, 30 July: 1-8, 9-15, 16-22, 23-29, 30-31

The predicate looks like

NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:self.pastStartDate endDate:self.currentEndDate options:HKQueryOptionStrictStartDate];

      

where self.pastStartDate

is the first of the last months, and self.currentEndDate

is the last of the current month.

Then we create a request like this

HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionDiscreteAverage anchorDate:self.pastStartDate intervalComponents:self.interval];

      

So, for a month is self.interval.day = 7

set in such a way that we retrieve data for a week for each interval. The problem is that this interval is not known to the calendar, so it doesn't know that the last month's data endpoint should only have 1 day, so we get some overlap.

We tried it too self.interval.weekOfMonth = 1

, but again, the interval doesn't know what month it is in, so that doesn't work either.

Is there a way to create a calendar-based interval so that it doesn't include more than the end of the month in a given interval?

+3


source to share





All Articles