I used to getting near real time heartrate from HealtKit written by apple watch work out until iOS13 in active way like below. Apps accesses HealthKit each time and getting near real time heartrate(almost updated each 5seconds).
But in iOS14, apps gets value each time, but it's never updated. I surely started apple watch workout and confirmed setting>healthkit>heartrate was updated each 5seconds heartrate. Then, once go background and back to foreground, apps gets updated heartrate value. But to get another updated value, you have to go background once.
How could it solve?
In addition, I tried passive way like below, same thing was happend.
But in iOS14, apps gets value each time, but it's never updated. I surely started apple watch workout and confirmed setting>healthkit>heartrate was updated each 5seconds heartrate. Then, once go background and back to foreground, apps gets updated heartrate value. But to get another updated value, you have to go background once.
How could it solve?
Code Block self.awTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.getHeartBeatFromAppleWatch), userInfo: nil, repeats: true) } @objc private func getHeartBeatFromAppleWatch() { let sortDescriptors = [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)] let query = HKSampleQuery.init(sampleType: self.hkTypeHeartRate, predicate: nil, limit: 1, sortDescriptors: sortDescriptors, resultsHandler: { (query, samples, error) -> Void in if let error = error { NSLog("[updateHeartRateNormal] resultHandler error: \(error.localizedDescription)") // error event } else if let samples = samples { // success event } }) self.healthStore.execute(query) }
In addition, I tried passive way like below, same thing was happend.
Code Block let distanceQuery = HKAnchoredObjectQuery(type: self.hkTypeHeartRate, predicate: .none, anchor: nil, limit: 0) { (query, samples, deletedObjects, anchor, error) -> Void in print(samples) } // This is called each time a new value is entered into HealthKit (samples may be batched together for efficiency) distanceQuery.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in print(samples) } // Start the query self.healthStore.execute(distanceQuery)