WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: HealthKit doesn't refresh query data until force quit

Forums > iOS


HealthKit is hard :)

Context: I'm building an app to keep track of mileage the user ran in their shoes. The user can manually log workouts, but I'm working on finishing up HealthKit integration so the user just has to log a workout in Health however they see fit and the app will find it.

HealthKit is connected and mostly working with one big exception.

Here is the issue I'm running into: If the user completes a workout and the app in either the background or foreground, the workout cannot be found until the user quits/reboots the app.

The below function I believe is the culprit. Specifically, the HKSampleQuery does not find workouts logged since the app launched.

Is there a way to find new workouts without quitting/restarting the app?

func asyncGetWorkouts(start: Date, end: Date) async -> [HKWorkout]? {
        let store = HKHealthStore()

        let walkingPredicate = HKQuery.predicateForWorkouts(with: .walking)        
        let timePredicate = HKQuery.predicateForSamples(withStart: start, end: end, options: .strictStartDate)

        let walkingCompound = NSCompoundPredicate(andPredicateWithSubpredicates:
        [walkingPredicate, timePredicate])

        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)

        func startQuery() async -> [HKWorkout] {
            let group = DispatchGroup()

            var output: [HKWorkout] = []
            let query = HKSampleQuery(sampleType: .workoutType(), predicate: walkingCompound, limit: 0, sortDescriptors: [sortDescriptor]) { (query, samples, error) in
                guard let samples = samples as? [HKWorkout], error == nil else {

                output = samples



            return output

        return await startQuery()


Is the end date in your query correct or is it only set on app start?


Thanks for the input and that is what it was! I was generating the date (which included time) when the app launched. Obviously the time was the issue, so now by updating the date upon calling this method fixed the bug!


Glad I could help.


according to my experienc the date in the queries is wrong. and that is creating such type of issue. As in the asparagus office I was analyzing such type of issue in an app and when I checked so the date was wrong in the script. Which was creating such type of issue. But when I made changes and fixed the date so the issue get fixed. Try to this method may be it also help you.


Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.