HKSampleQuery will only return values from past 7 days?
Asked Answered
C

1

6

This is a WatchOS App. Through testing, it appears as though this code will only return any bodyweight values that I manually add to the health app that are less than 1 week old. Is this intended? Ways around?

func getUserBodyMass(completion: @escaping (HKQuantitySample) -> Void) {

            guard let weightSampleType = HKSampleType.quantityType(forIdentifier: .bodyMass) else {
                print("Body Mass Sample Type is no longer available in HealthKit")
                return
            }

            //1. Use HKQuery to load the most recent samples.
            let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
                                                                  end: Date(),
                                                                  options: [])
            let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
                                                  ascending: false)
            let limit = 1
            let sampleQuery = HKSampleQuery(sampleType: weightSampleType,
                                            predicate: mostRecentPredicate,
                                            limit: limit,
                                            sortDescriptors: [sortDescriptor]) { (query, samples, error) in


                                                //2. Always dispatch to the main thread when complete.
                                                DispatchQueue.main.async {
                                                    guard let samples = samples,
                                                        let mostRecentSample = samples.first as? HKQuantitySample else {
                                                            print("getUserBodyMass sample is missing")
                                                            return
                                                    }
                                                    completion(mostRecentSample)
                                                }
            }
            healthStore.execute(sampleQuery)
    }
Crofton answered 10/2, 2018 at 5:35 Comment(2)
Are you running this code on watchOS?Glassware
@Glassware yes why?Crofton
G
10

HealthKit on watchOS only provides access to the last week of data. You can use the HKHealthStore method earliestPermittedSampleDate to query for the exact date. If you need to query for historical samples from HealthKit that may be more than a week old, you should do so with your companion iOS app and then send the relevant information to your watchOS app using WatchConnectivity.

Glassware answered 12/2, 2018 at 16:19 Comment(1)
Wish I've found your answer a couple of days before. It's definitely the right answer. I struggled around in searching for something broken inside my app, but at the end turns out that the problem was related to samples date. I manage to solve my problem by pushing the required value through WCSession. So, when the watch doesn't find anything, it could rely at least on a cached value.Curzon

© 2022 - 2024 — McMap. All rights reserved.