Query aggregate data #33
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If you use
query, you will probably find that the number of steps (or other types of data) do not match those shown by the Google Fit and Apple Health apps. If you wanted to accurately compute the user's data then use:queryAggregateDataiOS (Apple Health)
The
querymethod is over-counting steps because it simply sums the results of anHKSampleQuery. A sample query will return all samples matching the given predicate, including overlapping samples from multiple sources. If you wanted to accurately compute the user's step count usingHKSampleQuery, you'd have to detect overlapping samples and avoid counting them, which would be tedious and difficult to do correctly.Health uses
HKStatisticsQueryandHKStatisticsCollectionQueryto compute aggregate values. These queries calculate the sum (and other aggregate values) for you, and do so efficiently. Most importantly, though, they de-duplicate overlapping samples to avoid over-counting.The documentation for HKStatisticsCollectionQuery includes sample code.
Android (Google Fit)
The Fit app does some additional processing on top of the steps. It estimates steps based on the activity when none are recorded.
You need to use a custom
DataSourceof the packagecom.google.android.gms.You can find the solution on the Google Fit API FAQ page.