Apple - How does HealthKit resolve recording of the same activity by multiple sources?

HealthKit is pretty smart with your data. For every source in the Health app, you can set the priority of difference apps. Tap one of the metrics in the Health app tap Share data > Edit to drag sources in the right order.

If other apps request data, HealthKit will automatically merge the datasets, probably based on timestamps. It picks the high priority app first, and fills the gaps in data with the number 2 app, etc.

This WWDC would be a great start to get a complete overview.


As far as I researched the answer of Paul Veugen is only partially true. HealthKit does merge this Data if you use a HKStatisticsQuery or a HKStatisticsCollectionQuery. Otherwise it will be up to you, to provide a logic to merge/deduplicate the results.

One possible solution might be to use HKSourceQuery to Identify different sources and e.g. only select one.

Another might be to access the source via HKSourceRevision in the results of an HKQuery and only take the data from one source or try to compare data from the different sources. From iOS 9.3 there is also the possibility to access the different source devices using HKDevice.

Taking one device will always leave you with the possiblity, that an user did partially not use the device during the requested timespan, what will lead to missing datapoints.

Because of this priorising results will still remain painful, as Apple did not document, if the requested data will be sorted by the user defined source priority in Health.

Saying this, it might be worth looking into overlapping date ranges to identify duplicates.