Reputation: 89
I posted Setting up HKObserverQuery using Swift a few days ago.
I was wondering also, if there is a way to get the value of the most recent data point that was just added into HealthKit using the HKObserverQuery, or does it just let you know that there was a new data point added, but you still have to query it again using HKSampleQuery to retrieve the value.
Upvotes: 0
Views: 1043
Reputation: 278
The HKObserverQuery primarily notifies whether a sample type has been updated or not. For fetching the data you have to write your respective queries(like HKSampleQuery or HKAnchoredObjectQuery) in update handler block or closure. One thing you should take care of is the predicate provided to the observerquery, as it defines for how long it will observe the changes. The end date must be of future so that if there is change in any data it is notified, it defines till what time your observer will keep observing changes. If you want latest samples try using the HKAnchoredObjectQuery. Go through the definition of HKObserverQuery. Try not to set the end date more than one or two day. Hope this helps. The predicate part is important and a bit different than your predicate for sample queries. Make sure to call the ObserverQueryCompletionHandler if you are enabling background updates.
Upvotes: 3
Reputation: 5203
I was looking for the same answer and found this other post: Observing changes in HealthKit data using HKObserverQuery
It appears you do a separate query every time you get an update
Upvotes: 0