I implemented this to receive updates for specific data types and keep the latest daily information up to date. However, for some reason, it only works for a while before stopping completely.
Background Delivery
internal func backgroundDeliveryForReadTypes(enable: Bool, types: Set<HKQuantityType>) async {
do {
if enable {
try await statusForAuthorizationRequest(toWrite: [], toRead: types)
for type in types {
try await healthStore.enableBackgroundDelivery(for: type, frequency: .daily)
}
} else {
for type in types {
try await healthStore.disableBackgroundDelivery(for: type)
}
}
} catch {
debugPrint("Error enabling background delivery: \(error.localizedDescription)")
}
}
HKQueryAnchor
internal var walkingActivityQueryAnchor: HKQueryAnchor? {
get {
if let anchorData = UserDefaults.standard.data(forKey: "walkingActivityAnchor") {
return try? NSKeyedUnarchiver.unarchivedObject(ofClass: HKQueryAnchor.self, from: anchorData)
}
return nil
}
set {
if let newAnchor = newValue {
let anchorData = try? NSKeyedArchiver.archivedData(withRootObject: newAnchor, requiringSecureCoding: true)
UserDefaults.standard.set(anchorData, forKey: "walkingActivityAnchor")
} else {
UserDefaults.standard.removeObject(forKey: "walkingActivityAnchor")
}
}
}
HKAnchoredObjectQuery
internal func observeWalkingActivityInBackground(
_ start: Bool,
toRead: Set<HKQuantityType>,
completion: @escaping @Sendable (Result<WalkingActivityData?, Error>) -> Void
) {
if start {
guard (walkingActivityQuery == nil) else {
return
}
let predicate = getPredicate(date: Date())
let queryDescriptors = toRead.map {
HKQueryDescriptor(sampleType: $0, predicate: predicate)
}
let handleSamples: @Sendable (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = { [weak self] _, samples, _, newAnchor, error in
guard let self = self else { return }
if let error = error {
completion(.failure(error))
return
}
guard let samples = samples, !samples.isEmpty else {
completion(.success(nil))
return
}
Task {
self.walkingActivityQueryAnchor = newAnchor
let activity = await self.getWalkingActivity(date: Date())
completion(.success(activity))
}
}
let query = HKAnchoredObjectQuery(
queryDescriptors: queryDescriptors,
anchor: walkingActivityQueryAnchor,
limit: HKObjectQueryNoLimit,
resultsHandler: handleSamples
)
query.updateHandler = handleSamples
healthStore.execute(query)
walkingActivityQuery = query
} else {
if let query = walkingActivityQuery {
healthStore.stop(query)
walkingActivityQuery = nil
}
}
}
WalkingActivityData
private func getWalkingActivity(date: Date) async -> WalkingActivityData {
async let averageHeartRate = try await self.getAverageHeartRate(date: date)
async let steps = try self.getStepCount(date: date)
async let durationMinutes = try self.getTotalDurationInMinutes(date: date)
async let distanceMeters = try self.getDistanceWalkingRunning(date: date, unit: .meter())
async let activeCalories = try self.getActiveEnergyBurned(date: date)
return await WalkingActivityData(
date: date,
steps: try? steps,
activeCalories: try? activeCalories,
distanceMeters: try? distanceMeters,
durationMinutes: try? durationMinutes,
averageHeartRate: try? averageHeartRate
)
}
Example of getAverageHeartRate
func getAverageHeartRate(date: Date) async throws -> Double? {
let type = HKQuantityType(.heartRate)
_ = try checkAuthorizationStatus(for: type)
guard let heartRate = try await getDescriptor(
date: date,
type: type,
options: .discreteAverage
).result(for: healthStore)
.statistics(for: date)?
.averageQuantity()?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
else {
return nil
}
return Double(String(format: "%.2f", heartRate)) ?? 0.0
}
Descriptor & predicate
internal func getPredicate(startDate: Date, endDate: Date) -> NSCompoundPredicate {
let predicateForSamples = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
let excludeManual = NSPredicate(format: "metadata.%K != YES", HKMetadataKeyWasUserEntered)
return NSCompoundPredicate(andPredicateWithSubpredicates: [predicateForSamples, excludeManual])
}
internal func getDescriptor(startDate: Date, endDate: Date, type: HKQuantityType, options: HKStatisticsOptions) -> HKStatisticsCollectionQueryDescriptor {
let calendar = Calendar(identifier: .gregorian)
let anchorDate = calendar.date(bySetting: .hour, value: 0, of: startDate)!
var interval = DateComponents()
interval.day = 1
return HKStatisticsCollectionQueryDescriptor(
predicate: HKSamplePredicate.quantitySample(type: type, predicate: getPredicate(startDate: startDate, endDate: endDate)),
options: options,
anchorDate: anchorDate,
intervalComponents: interval
)
}
Implementation
public func observeWalkingActivityInBackground(_ start: Bool, toRead: Set<HKQuantityType>, memberID: String) {
observeWalkingActivityInBackground(start, toRead: toRead) { [weak self] result in
guard let self = self else { return }
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I’m trying to use BGProcessingTaskRequest to fetch step data in the background and send it. However, when I combine BGProcessingTaskRequest, HKObserverQuery, and healthStore.enableBackgroundDelivery, the results sometimes return zero. When I don’t schedule the BGProcessingTaskRequest, the data retrieved using HKObserverQuery and HKSampleQueryDescriptor is correct.
// Register Smart Walking Sync Task
func registerSmartWalkingSync() {
#if !targetEnvironment(simulator)
BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTaskIdentifier.smartwalking.rawValue, using: nil) { task in
guard let task = task as? BGProcessingTask else { return }
self.handleSmartWalkingSync(task: task)
}
#endif
}
func scheduleSmartWalkingSync(in seconds: TimeInterval? = nil, at date: Date? = nil) {
let newRequest = BGProcessingTaskRequest(identifier: BGTaskIdentifier.smartwalking.rawValue)
newRequest.requiresNetworkConnectivity = true
newRequest.requiresExternalPower = false
if let seconds = seconds {
newRequest.earliestBeginDate = Date().addingTimeInterval(seconds)
} else if let date = date {
newRequest.earliestBeginDate = date
}
do {
try BGTaskScheduler.shared.submit(newRequest)
debugPrint("✅ [BGTasksManager] scheduled for Smart Walking Sync")
} catch {
FirebaseConnection.shared.recordException(error)
debugPrint("❌ [BGTasksManager] error: \(error)")
}
}
// Handle Smart Walking Sync Task
func handleSmartWalkingSync(task: BGProcessingTask) {
debugPrint("🔄 [BGTasksManager] sync \(task.identifier) sync started")
scheduleSmartWalkingSync(in: SYNC_SMARTWALKING_TIME_INTERVAL)
let queue = OperationQueue()
let operation = HealthActivitiesOperation()
operation.completionBlock = {
Task {
do {
try await operation.sync()
task.setTaskCompleted(success: !operation.isCancelled)
debugPrint("✅ [BGTasksManager] sync \(task.identifier) completed successfully")
} catch {
FirebaseConnection.shared.recordException(error)
task.setTaskCompleted(success: false)
debugPrint("❌ [BGTasksManager] sync \(task.identifier) error: \(error)")
}
}
}
task.expirationHandler = {
operation.cancel()
}
queue.addOperation(operation)
}
// MARK: - HealthKit Background Delivery
internal func enableBackgroundDeliveryForAllTypes() async throws {
for type in allTypes.filter({ type in
type != HKQuantityType(.heartRate)
}) {
try await healthStore.enableBackgroundDelivery(for: type, frequency: .daily)
}
debugPrint("✅ [HealthKitManager] Enable Background Delivery")
}
internal func observeHealthKitQuery(predicate: NSPredicate?) async throws -> Set<HKSampleType> {
let queryDescriptors: [HKQueryDescriptor] = allTypes
.map { type in
HKQueryDescriptor(sampleType: type, predicate: predicate)
}
return try await withCheckedThrowingContinuation { continuation in
var hasResumed = false
let query = HKObserverQuery(queryDescriptors: queryDescriptors) { query, updatedSampleTypes, completionHandler, error in
if hasResumed {
return
}
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: updatedSampleTypes ?? [])
}
hasResumed = true
completionHandler()
}
healthStore.execute(query)
}
}
internal func getHealthActivity(by date: Date, predicate: NSCompoundPredicate, sampleTypes: Set<HKSampleType>) async throws -> HealthActivityData {
var data = HealthActivityData(steps: 0, calories: 0, distance: 0.0, distanceCycling: 0.0, totalDuration: 0, date: date, heartRate: nil)
for sampleType in sampleTypes {
guard let quantityType = sampleType as? HKQuantityType else {
continue
}
switch quantityType {
case HKQuantityType(.stepCount):
let stepCount = try await getDescriptor(
date: date,
type: quantityType
).result(for: healthStore)
.statistics(for: date)?.sumQuantity()?.doubleValue(for: HKUnit.count())
data.steps = stepCount ?? 0.0
// Calculate total duration using HKSampleQueryDescriptor
let totalDurationDescriptor = HKSampleQueryDescriptor(
predicates: [.quantitySample(type: quantityType, predicate: predicate)],
sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)]
)
let stepSamples = try await totalDurationDescriptor.result(for: healthStore)
data.totalDuration += stepSamples
.reduce(0) { $0 + $1.endDate.timeIntervalSince($1.startDate) } / 60.0
default:
debugPrint("Unknown quantity type")
}
}
return data
}