diff --git a/Sources/SpeziHealthKit/Health Data Collection/HealthKitSampleCollector.swift b/Sources/SpeziHealthKit/Health Data Collection/HealthKitSampleCollector.swift index f84bf97..d261b85 100644 --- a/Sources/SpeziHealthKit/Health Data Collection/HealthKitSampleCollector.swift +++ b/Sources/SpeziHealthKit/Health Data Collection/HealthKitSampleCollector.swift @@ -91,22 +91,29 @@ final class HealthKitSampleCollector: 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) diff --git a/Tests/UITests/TestApp.xctestplan b/Tests/UITests/TestApp.xctestplan index 21e3b88..03ff6d0 100644 --- a/Tests/UITests/TestApp.xctestplan +++ b/Tests/UITests/TestApp.xctestplan @@ -9,6 +9,20 @@ } ], "defaultOptions" : { + "codeCoverage" : { + "targets" : [ + { + "containerPath" : "container:..\/..", + "identifier" : "SpeziHealthKit", + "name" : "SpeziHealthKit" + }, + { + "containerPath" : "container:..\/..", + "identifier" : "SpeziHealthKitUI", + "name" : "SpeziHealthKitUI" + } + ] + }, "commandLineArgumentEntries" : [ { "argument" : "-AppleLocale",