Is there a way to remove duplicates in HKSampleQuery?

Hello all!


I have been using HealthKit to collect some health data, and I also need to collect information about the device that the data came from.


If I use HKSampleQuery, HealthKit returns all data available from all sources (e.g. steps data from iPhone and Apple Watch). In this case, I can get the device from which the sample originated, but I will also receive duplicate data and will have to create some kind of internal logic to identify it.


If I use a different query, such as HKStatisticsCollectionQuery, I will be able to get the consolidated data, without duplicates, but I will not have the device information. I can use the option .separateBySource and get the source information, but the HKSource class has considerably less information than the device class, HKDevice.


Am I missing option? Is there a simple way to remove duplicates in HKSampleQuery, while identifying and acting upon duplicate information?


Thank you!

To remove duplicates if you have an array of samples, you could create a Set with it


let uniqueSamples = Set(samples)


But you should be aware tha, a Set is an unordered collection of unique elements.

Did anyone solved it, I really want to show my sleep data just like HealthKit sleep data, but it got duplicate after using multiple data source at same start time and end time
Is there a way to remove duplicates in HKSampleQuery?
 
 
Q