Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where exactly is Heart Rate Data in Fitness activity results #530

Closed
khanxc opened this issue Oct 14, 2015 · 3 comments
Closed

Where exactly is Heart Rate Data in Fitness activity results #530

khanxc opened this issue Oct 14, 2015 · 3 comments

Comments

@khanxc
Copy link

khanxc commented Oct 14, 2015

This is basically not an issue. Where exactly will I get the heart rate data while using the Fitness activity ? Even I don't see the Heart Rate identifier in the below code

    if let fileresult = result as? ORKFileResult {
                             print("idf -->\(fileresult.identifier)")
                                let content = try? String(contentsOfURL: fileresult.fileURL!, encoding: NSUTF8StringEncoding)
                                print("File contents\(content)")
 } 

And I heart rate was stored continuously to HealthKit through an app in apple watch at the same time of taking the fitness test.
img_0004

@YuanZhu-apple
Copy link
Member

I believe there is a few minutes delay to sync heart rate data from Apple Watch to iPhone's HealthKit.
Did you try to use a heart rate strap? Remember to pair it with iPhone directly (settings -> bluetooth).

And if the heart rate data is in real time, then you should be able to see a heart rate label at the bottom on the walking step.

@khanxc
Copy link
Author

khanxc commented Oct 15, 2015

@YuanZhu-apple Thanks for your response. I did not try with a heart rate strap. But through Apple Watch doesn't HKWorkout class provide real time data?. I tried with this below code

self.workoutSession = HKWorkoutSession(activityType: .Running, locationType: .Indoor)
self.workoutSession!.delegate = self;

// Start the workout session
self.healthStore.startWorkoutSession(self.workoutSession!)

And HKQuery output seems to be near to real time in the Apple watch. So if there is even a delay by a minute RK doesn't fetch it?

@lehn0058
Copy link

If you have real time health data collected from the Apple Watch and are trying to query it from it's paired iPhone, you will likely have some trouble. The Apple Watch doesn't stream fitness data in real time to the iPhone because this would be a huge battery drain, especially for long workouts (think of runners training for a marathon where they could be exercising for over 5 hours). The Apple watch batches up its health kit data and syncs it with the iPhone at opportune times. Normally, this happens within a few minutes of completing a workout. I have noticed that if I want to view HealthKit data immediately after completing a workout on the apple watch I need to open the health app on the iPhone to manually trigger a sync.

One thing to try is to make sure you are relating your health samples to the workout you end up recording. If you just let the apple watch collect the samples, it won't know that they belong to a specific workout. This is how health results like average heart rate and splits show up in the activity app.

// Create a workout sample
let workout = HKWorkout(activityType: HKWorkoutActivityType.Running,
    startDate: startDate,
    endDate: endDate,
    duration: endDate.timeIntervalSinceDate(startDate),
    totalEnergyBurned: self.currentActiveEnergyBurned,
    totalDistance: self.currentDistance,
    metadata: nil)

// Save the workout
self.healthStore.saveObject(workout) { success, error in
    // Associate the accumulated samples with the workout
    if success {

        let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.None)
        let startDateSort = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)

        let activeEnergyQuery = HKSampleQuery(sampleType: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!, predicate: predicate,
            limit: 0, sortDescriptors: [startDateSort]) {
                (sampleQuery, results, error) -> Void in

                if results?.count > 0 {
                    self.healthStore.addSamples(results!, toWorkout: workout, completion: { success, error in
                        // ...
                    })
                }
        }

        let heartRateQuery = HKSampleQuery(sampleType: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!, predicate: predicate,
            limit: 0, sortDescriptors: [startDateSort]) {
                (sampleQuery, results, error) -> Void in

                if results?.count > 0 {
                    self.healthStore.addSamples(results!, toWorkout: workout, completion: { success, error in
                        // ...
                    })
                }
        }

        let distanceQuery = HKSampleQuery(sampleType: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!, predicate: predicate,
            limit: 0, sortDescriptors: [startDateSort]) {
                (sampleQuery, results, error) -> Void in

                if results?.count > 0 {
                    self.healthStore.addSamples(results!, toWorkout: workout, completion: { success, error in
                        // ...
                    })
                }
        }

        self.healthStore.executeQuery(activeEnergyQuery)
        self.healthStore.executeQuery(heartRateQuery)
        self.healthStore.executeQuery(distanceQuery)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants