Walking/Running Distance not included in daily total

Hi, I have an iPhone/watch app that measures walking/running distance. It starts a workout session records data successfully into healthkit and then ends the workout session. I can see the data in the Health app from my app and it all appears to be correct. The issue I'm having is that the Health app doesn't use my app's data in its total distance for the day. For example, I can add 1 mile of fake data (I know that's not allowed per policy) for testing and it gets successfully added but not used in the total. The health app is only using distances collected by the watch and the phone for calculating the total distance for the day and is ignoring the distance provided by my app. What could I be missing?

I just tried adding a workout in the iOS Health.app (Browse > Activity > Workouts > Add Data), and also tried adding a workout with my app, and my following code returns the distance that includes the workout distance I added:

let sampleType = HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!
let predicate = HKQuery.predicateForSamples(withStart: Date(timeIntervalSinceNow: -24 * 60 * 60), end: Date())
let statisticsQuery = HKStatisticsQuery(quantityType: sampleType,
                                        quantitySamplePredicate: predicate,
                                        options: [.cumulativeSum]) { query, statistics, error in
    if let error = error {
        print("Failed to run statistics query: \(error)")
        return
    }
    if let quantity = statistics?.sumQuantity() {
        let distanceWalkingRunning = quantity.doubleValue(for: .mile())
        print("distanceWalkingRunning = \(distanceWalkingRunning)")
    }
}
healthStore.execute(statisticsQuery)

So would you mind to elaborate how you add a workout and how you observe the distance? I'm curious how you did differently.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi, and thanks for your reply. I don't believe my code has any issues with adding the workout nor adding the distance into the Walking + Running Distance portion of the Health.app. I can browse the data in those sections of the Health.app and see all of the data from my app. However, if I go to Browse > Activity > Walking + Running Distance and tap on the bar in the chart for the current day the total distance (miles in my case) for the current day does not include the distance that I've added. If I scroll to the bottom of this section and select Show All Data I can see the data from my application. I have also gone in to the Data Sources & Access section and moved my app to the top of the list of Data Sources with no luck. So to summarize, the workout data and distance data from my app are appearing correctly in Health.app, but the distance reported by my app is not being used in the total for the day as reported by the chart at the top of the Walking + Running Distance section.

Walking/Running Distance not included in daily total
 
 
Q