Reputation: 6475
Neither class reference nor any tutorial available online say anything about caching anchors between application runs.
I want to query data between app runs so I get all historical HealthKit
entries. At first, I thought I'd need to store last query NSDate
in NSUserDefaults
and on the next app run I'll just hit this value and set it as startDate
of HKSampleQuery
but after some reading I came onto this cool thing called HKAnchoredObjectQuery
which should be handling this caching for me so whenever I hit I'll get only new entries.
Does this class automatically store anchors or it just stores it on app run and when app is terminated everything gets cleared and I'm starting again with HKAnchoredObjectQueryNoAnchor
?
Upvotes: 1
Views: 688
Reputation: 7363
Your app must store the HKQueryAnchor itself. HKAnchoredObjectQuery can't cache it across app launches for you - how would HealthKit know that you were performing the same query for the same purpose?
Upvotes: 4
Reputation: 6475
After doing some experiments in the code it looks like this anchor is a regular Int
(<9.0) or HKQueryAnchor
(>=9.0) and it holds value only when app is in froeground.
If you (like me) want to keep this value between app runs you'll have to store it manually for example in NSUserDefaults
.
I'm still not fully confident that they can keep an eye on this only based on simple Int
value... More reliable solution for me is to manually store NSDate
of last query execution and based on this query items only from this range of dates.
One additional thing which may be helpful - HKAnchoredObjectQuery
automatically sorts returned data in ascending order.
Upvotes: 0