If I query heart rate samples for a recent workout, it being yesterday or the past month for example, I get the full samples. If I query a workout let's say, two/three months in the past the samples I get back look "choppy" with a lot of missing data. (See attached image)
imgur.com/a/FbMSBoa
How am I getting the heart rate samples?
Code Block swift extension HKHealthStore: HKHealthStoreCombine { public func get<T>(sample: T, start: Date, end: Date, limit: Int = HKObjectQueryNoLimit) -> AnyPublisher<[HKQuantitySample], Error> where T: HKObjectType { let subject = PassthroughSubject<[HKQuantitySample], Error>() let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier(rawValue: sample.identifier))! let predicate = HKQuery.predicateForSamples(withStart: start, end: end) let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: limit, sortDescriptors: nil, resultsHandler: { (query, samples, error) in guard error == nil else { logger.error("Error fetching samples of type \(sample.description) from \(start) to \(end) with a limit of \(limit): \(error!.localizedDescription)") subject.send(completion: .failure(error!)) return } let samples = samples as? [HKQuantitySample] ?? [] logger.log("Successfully fetched \(samples.count) samples of type \(sample.description) from \(start) to \(end) with a limit of \(limit)") subject.send(samples) subject.send(completion: .finished) }) self.execute(query) return subject.eraseToAnyPublisher() } }
Code Block swift healthStore.get(sample: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!, start: workout.startDate, end: workout.endDate) .map({ $0.compactMap({ $0.quantity.doubleValue(for: UserUnits.shared().heartCountUnits) }) }) .replaceError(with: []) .receive(on: DispatchQueue.main) .sink(receiveValue: { self.heartRate = $0 }) .store(in: &bag)
Is this some kind of wrong way that I am working with HealthKit? I don't know what the issue can be but it looks like accessing heart samples in the "past" are somewhat archived and I don't get the full resolution.