I don't have a way around the locked-store restriction either. There's now an explicit DTS answer confirming reads aren't permitted while locked (thread 824819, May 2026), so I've stopped hoping for one.
What I can offer is the failure mode I hit while working around it, because it's easy to ship by accident and it presents as a completely different bug.
A statistics query that fails because the store is encrypted reports no sum. That is the same thing it reports for a day with genuinely no samples. So if your read does something like
result?.sumQuantity()?.doubleValue(for: unit) ?? 0
and binds the error parameter to _, a failed read returns a confident 0. Mine then got published to the widget with a current timestamp, so every morning the widget showed a plausible, freshly-stamped, completely wrong zero until the app was next opened. It reads exactly like a refresh bug — I lost time on timeline reloads and App Group configuration before noticing the timestamp was fresh, which meant the data wasn't stale, it was freshly wrong.
What worked, given we can't read while locked:
Return an optional from the read. nil for a failed query, and 0 only for HKError.errorNoData, which is HealthKit's way of saying the range really is empty.
On failure, publish nothing and leave the previous values untouched. A widget showing yesterday's number is far better than one showing a wrong number today.
Audit every path that writes to the widget, not just the one you fixed. A freshly-initialized model object is full of zeros too, and those are every bit as plausible as the ones a failed query produces.
Carry the day the numbers describe separately from the "updated at" time. Once a write can be skipped, "when was this written" stops answering "which day is this about" — and that distinction is what lets you render a stale payload as visibly stale instead of asserting it.
None of that gets data while locked. It does stop the lock from producing wrong data, which in my case was the actual user-visible bug.
Longer version: https://mcmizzle.com/blog/healthkit-widget-shows-zero/
Topic:
App & System Services
SubTopic:
Health & Fitness
Tags: