Skip to content

Commit

Permalink
Monitor HealthKit data changes via HKObserverQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryashutov committed Nov 23, 2023
1 parent 3cbb3c6 commit 7b15688
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions ios/FHIRmHealth/HealthKitConnector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ enum HealthKitConnectorError: Error {
@objc
class HealthKitConnector: NSObject {
private let store = HKHealthStore()
private var runningQuery: HKAnchoredObjectQuery? = nil
private var runningQuery: HKObserverQuery? = nil
private var observerQueryCompletionHandler: HKObserverQueryCompletionHandler? = nil
private var queryAnchor: HKQueryAnchor? = HealthKitHistory.restore()
private var eventChannels: [HealthKitEventChannel] = []
private var eventDeliveryQueue: [(HealthKitEvent, String?)] = []
Expand Down Expand Up @@ -96,22 +97,33 @@ class HealthKitConnector: NSObject {
let timeLimitPredicate = HKQuery.predicateForSamples(
withStart: Calendar.current.date(byAdding: .day, value: -7, to: .now), end: nil, options: []
)
let anchoredQuery = HKAnchoredObjectQuery(type: HKQuantityType.workoutType(),
predicate: timeLimitPredicate,
anchor: self.queryAnchor,
limit: HKObjectQueryNoLimit) {_, created, removed, anchor, error in
self.storeUpdateHandler(created, removed, anchor, error)
}
anchoredQuery.updateHandler = {_, created, removed, anchor, error in
self.storeUpdateHandler(created, removed, anchor, error)
let query = HKObserverQuery(sampleType: HKQuantityType.workoutType(),
predicate: timeLimitPredicate) { (query, completionHandler, errorOrNil) in
if let error = errorOrNil {
logger.error("Observer query has failed with error: \(error)")
completionHandler()
return
}
let anchoredQuery = HKAnchoredObjectQuery(type: HKQuantityType.workoutType(),
predicate: timeLimitPredicate,
anchor: self.queryAnchor,
limit: HKObjectQueryNoLimit) {_, created, removed, anchor, error in
self.storeUpdateHandler(created, removed, anchor, error)
}
anchoredQuery.updateHandler = {_, created, removed, anchor, error in
self.storeUpdateHandler(created, removed, anchor, error)
}
self.store.execute(anchoredQuery)
self.observerQueryCompletionHandler = completionHandler
}
self.store.execute(anchoredQuery)
self.store.execute(query)
self.runningQuery = query
self.store.enableBackgroundDelivery(for: HKQuantityType.workoutType(), frequency: .immediate) {_,error in
if error != nil {
logger.error("Failed to enabled background updates delivery: \(error)")
}
}
self.runningQuery = anchoredQuery

self.distribute(event: .queryStatusHasChanged(.running))
}
}
Expand All @@ -134,6 +146,10 @@ class HealthKitConnector: NSObject {
if let anchor = try? HKQueryAnchor.deserialise(from: transaction) {
HealthKitHistory.checkpoint(at: anchor)
}

if let observerQueryCompletionHandler = self.observerQueryCompletionHandler {
observerQueryCompletionHandler()
}
}

func queryActivitySummary(_ completion: @escaping (HKActivitySummary?) -> Void) throws -> Void {
Expand Down

0 comments on commit 7b15688

Please sign in to comment.