I have moved your post to the API Forum @pawan-sharma.
My thought is that since your getting data from two different sights, duplicates would need to be determined by your logic.
Someone here might know of another trick.
Best AnswerUse metadata to identify your records and let Apple Health de-duplicate. Here is the method I use to create my samples. Note the metadata dictionary. HKMetadataKeySyncIdentifier is a string that is constant for each datapoint. I change HKMetadataKeySyncVersion when my data changes. I assume my data is always changing, and use the number of seconds since epoch to provide an ever-increasing version number.
private func createHealthKitSample(date: String, time: String, quantity: Double, unit: HKUnit, type: HKQuantityType) -> HKQuantitySample {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// TODO Correct times to UTC by combining timezone offset from profile
let start = dateFormatter.date(from: "\(date) \(time)")!
let end = start.addingTimeInterval(60)
let metadata = [
HKMetadataKeySyncIdentifier: "fitbit-connector-\(type.identifier)-\(start)",
HKMetadataKeySyncVersion: Date().timeIntervalSince1970
] as [String : Any]
return HKQuantitySample(
type: type,
quantity: HKQuantity(unit: unit, doubleValue: quantity),
start: start,
end: end,
metadata: metadata
)
}
Best Answer