Reputation: 101
I am using a method similar to the Swift example in Get total step count for every date in HealthKit to acquire the number of steps from HealthKit. That works great.
My preference would be to get the number of steps per minute or per hour though instead of the per day that that code does -- While the sum of hourly steps perfectly matches the daily step count reported by HealthKit, the sum of minutely steps does not match hourly or daily sums.
Is there a way to get per-minute step summaries work? Or is there a logical answer as to why they are vastly different?
The only differences from the code above and my code is the following for Per Hour calculations (works the same):
interval.hour = 1
var anchorComponents = calendar.dateComponents([.hour, .day, .month, .year], from: NSDate() as Date)
and the following per minute calculations (usually over counts):
interval.minute = 1
var anchorComponents = calendar.dateComponents([.minute, .hour, .day, .month, .year], from: NSDate() as Date)
Clearly I am missing something. Thanks for any insight.
Eric
Upvotes: 2
Views: 1015
Reputation: 464
There are options (.strictStartDate, .strictEndDate) for the query predicate that determine whether the query finds all samples that are strictly within the interval (strict stop and start date), or whether it will include samples that stop or start outside of the interval.
My suspicion is that your step samples may cross minute boundaries, and depending on how you define the interval, this will make a significant difference.
I work with daily summaries and have defined the interval to be (strict, not-strict) so that a sample that spans two days is only counted in the one that it starts in. (Swift 4)
let predicate = HKQuery.predicateForSamples(withStart: start, end: end, options: .strictStartDate)
I note that the final answer in the SO question you linked to defines a (strict, not-strict) interval as well.
Upvotes: 2