Skip to content

Commit

Permalink
improve background query handling (#38)
Browse files Browse the repository at this point in the history
# improve background query handling

## ♻️ Current situation & Problem
HealthKit sometimes sends a background query notification even though no
new samples were added to the database (i've seen this happen in e.g.
the simulator, my guess is that in that case even though there simply
aren't any samples it for some reason still wants to send one initial
query update).
This interferes a bit with our existing sample type correctness checking
(there is a check that the array of new samples contains at least one
sample with the queried-for sample type, which is of course false in
this case).
This PR reworks this a bit, essentially simply adding a check to return
early if the array is empty.

## ⚙️ Release Notes 
- improved handling of background queries to avoid unnecessary error
messages printed to stdout

## 📚 Documentation
n/a

## ✅ Testing
n/a

## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).

---------

Signed-off-by: Lukas Kollmer <hey@lukaskollmer.de>
  • Loading branch information
lukaskollmer authored Mar 8, 2025
1 parent 35e4a4a commit 6b399ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,29 @@ final class HealthKitSampleCollector<Sample: _HKSampleWithSampleType>: HealthDat
// if the sample collector has been turned off, we don't want to process these.
return
}
guard case let .success((sampleTypes, completionHandler)) = result else {
return
}
guard sampleTypes.contains(self.sampleType.hkSampleType) else {
self.healthKit.logger.warning("Received Observation query types (\(sampleTypes)) are not corresponding to the CollectSample type \(self.sampleType.hkSampleType)")
completionHandler()
return
}
do {
try await self.anchoredSingleObjectQuery()
self.healthKit.logger.debug("Successfully processed background update for \(self.sampleType.hkSampleType)")
} catch {
self.healthKit.logger.error("Could not query samples in a background update for \(self.sampleType.hkSampleType): \(error)")
switch result {
case .failure(let error):
self.healthKit.logger.error("Error in background delivery: \(error)")
case let .success((sampleTypes, completionHandler)):
defer {
// Inform to HealthKit that the data has been processed:
// https://developer.apple.com/documentation/healthkit/hkobserverquerycompletionhandler
completionHandler()
}
guard !sampleTypes.isEmpty else {
return
}
guard sampleTypes.contains(self.sampleType.hkSampleType) else {
self.healthKit.logger.warning("Received Observation query types (\(sampleTypes)) are not corresponding to the CollectSample type \(self.sampleType.hkSampleType)")
return
}
do {
try await self.anchoredSingleObjectQuery()
self.healthKit.logger.debug("Successfully processed background update for \(self.sampleType.hkSampleType)")
} catch {
self.healthKit.logger.error("Could not query samples in a background update for \(self.sampleType.hkSampleType): \(error)")
}
}
// Provide feedback to HealthKit that the data has been processed: https://developer.apple.com/documentation/healthkit/hkobserverquerycompletionhandler
completionHandler()
}
isActive = true
queryVariant = .backgroundDelivery(queryInvalidator)
Expand Down
14 changes: 14 additions & 0 deletions Tests/UITests/TestApp.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
}
],
"defaultOptions" : {
"codeCoverage" : {
"targets" : [
{
"containerPath" : "container:..\/..",
"identifier" : "SpeziHealthKit",
"name" : "SpeziHealthKit"
},
{
"containerPath" : "container:..\/..",
"identifier" : "SpeziHealthKitUI",
"name" : "SpeziHealthKitUI"
}
]
},
"commandLineArgumentEntries" : [
{
"argument" : "-AppleLocale",
Expand Down

0 comments on commit 6b399ad

Please # to comment.