HKAnchoredObjectQuery: unexpected behaviour

Hi,


I am making an app that imports data from HealthKit. For that, I have implemented a strategy using HKAnchoredObjectQuery.


My strategy has two state: Syncing and Updating.
When the app is started for the first time, and HealthKit is enabled, my importer starts in Syncing state. The sole reason for this is to be able to import all the existing HealthKit data in small batches (50 samples at a time). So I create a HKAnchoredObjectQuery to fetch the first bunch of samples (in this case BloodPressure correlations). The query object is initialised like this:
```
HKAnchoredObjectQuery(type:HKObjectType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure),
predicate: nil,
anchor: nil,
limit: 50,
resultsHandler: resultsHandler)
```
I have about 1500 bloodpressure samples in HealthKit.
My strategy keeps doing this anchored object query with the previously returned anchor, until I receive 0 for samples and deletedSamples. Then the strategy is to move into an Updating state.
In Updating state, I configure the anchored query as a long-running Update query with an updateHandler, and also still provide a resultsHandler (for the case I missed any new samples between the last Syncing query and the new Updating query. I pass a limit of HKObjectQueryNoLimit, as the API does not allow me to give a limit for update queries.
Now, this works great. Except on ONE phone that I have. On this phone, the query that I fire in Syncing state, does not return 50 items, but only 1 item. And the next query I do with the new anchor return 0 items. And so my strategy thinks that all items have been synced and can go into the Updating state.
However, in the Updating state, the first thing that happens is that my resultsHandler is called with all the bloodpressure samples (minus the one I received in Syncing state). So suddenly I'm processing 1500 samples, instead of a number of batches of 50!
This is happening on ONLY one phone. What can be the problem? If this is happening to my phone, then it can also happen to my customer's phones. So I have to get to the bottom of this.


Did I mess up the anchor system that Apple employs? Is there any way to reset that anchoring system (other than reinstall Apple Health)?
My phone is an iPhone 8 Plus, running iOS 12.1.4, and using Xcode 10.2.
So to make it clear: it works fine in the simulator, it works fine on another phone, and it USED to work fine on my own phone. But it's now messed up on my own phone.

Any help is appreciated. Even speculation.
Cheers!
Peter

The documentation says this:
"If the

myAnchor
property is set to
nil
, the query returns all the step count data currently available in the HealthKit store. On subsequent runs, the code uses the anchor returned by the previous query. These queries return only the step count samples that have been saved or deleted since the previous run."

So, am I wrong in assuming that if I make an initial query with the anchor set to nil, and a limit of 50, I would get the first 50 samples available in the HealthKit store? And if I use the returned anchor, and do a next query with a limit of 50, I would get the next 50 samples available in the HealthKit store?
I get the feeling that I'm only getting all the data currently available in the HealthKit store if I set the limit to

HKObjectQueryNoLimit
. If I set the limit to anything else, it seems like I do get SOME of the data, but never ALL of the data. In fact, it seems like the AnchoredQuery with a limit works more like an update query with a limited number of returned items, than something that returns ALL the data.
The documentation doesn't mention anything about this.
HKAnchoredObjectQuery: unexpected behaviour
 
 
Q