Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Sync Heart Rate Data with Apple Health

Hey, I am developing an app which shows data from both Apple Health and Fitbit web api. I am seeing duplicate data. Is there any way to know that data is duplicate and hide it.

Best Answer
0 Votes
2 REPLIES 2

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 Answer
0 Votes

Use 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
0 Votes