I'm having the same issue. Is there any way to fix it?
Post
Replies
Boosts
Views
Activity
Hi @eschos24!
I still have the problem but found a little workaround by building a semaphore using Swift Actor's. So the import function only fires once every 10 seconds.
actor Semaphore {
static let shared = Semaphore()
var isImporting = false
func run(sleeping: UInt32 = 5, function: @escaping () async -> Void) async {
if !isImporting {
isImporting = true
await function()
sleep(sleeping)
isImporting = false
}
}
}
This is it in action
private func setUpBackgroundDelivery() async {
var queryDescriptors = [HKQueryDescriptor]()
for type in types {
queryDescriptors.append(HKQueryDescriptor(sampleType: type, predicate: nil))
}
let observerQuery = HKObserverQuery(queryDescriptors: queryDescriptors) { query, sampleTypesWithNewData, completionHandler, error in
if let error = error {
completionHandler()
return
}
Task {
await self.semaphore.run(sleeping: 10) {
await self.checkForNewWorkouts()
}
completionHandler()
}
}
HKStore.execute(observerQuery)
do {
try await HKStore.enableBackgroundDelivery(for: workout, frequency: .hourly)
} catch {
}
}
I also tried with:
func setUpBackgroundDelivery() async {
var queryDescriptors = [HKQueryDescriptor]()
for type in types {
queryDescriptors.append(HKQueryDescriptor(sampleType: type, predicate: nil))
}
let observerQuery = HKObserverQuery(queryDescriptors: queryDescriptors) { query, sampleTypesWithNewData, completionHandler, error in
if let error = error {
self.logger.error("HKObserverQuery error: \(error.localizedDescription)")
completionHandler()
return
}
for type in sampleTypesWithNewData! {
self.logger.debug("New \(type.debugDescription) data")
self.anchoredQueryFor(sampleTypesWithNewData!)
}
completionHandler()
}
HKStore.execute(observerQuery)
for type in types {
do {
try await HKStore.enableBackgroundDelivery(for: type, frequency: .hourly)
} catch {
self.logger.error("setUpBackgroundDeliveryFor \(type) - error: \(error.localizedDescription)")
}
}
}
And the update handler gets called twice:
2022-06-10 10:54:06.464318+0200 Cori[81627:9010600] [HealthKit] New HKQuantityTypeIdentifierBloodGlucose data
2022-06-10 10:54:06.467039+0200 Cori[81627:9010600] [HealthKit] New HKQuantityTypeIdentifierBloodGlucose data
2022-06-10 10:54:06.472524+0200 Cori[81627:9010601] [HealthKit] New data: [222 mg/dL 6AA78B15-4EB5-4F80-823E-DFBC43AB3C5A "Salud" (15.5), "iPhone14,2" (15.5)metadata: {
2022-06-10 10:54:06.472742+0200 Cori[81627:9010601] [HealthKit] 1 new HKQuantityTypeIdentifierBloodGlucose samples
2022-06-10 10:54:06.472889+0200 Cori[81627:9010591] [persistence] HealthKit: Start import to Core Data
2022-06-10 10:54:06.473032+0200 Cori[81627:9010591] [persistence] HealthKit: 1 samples of type HKQuantityTypeIdentifierBloodGlucose not from this app!
2022-06-10 10:54:06.473103+0200 Cori[81627:9010591] [persistence] HealthKit: Start batch insert request.
2022-06-10 10:54:06.473356+0200 Cori[81627:9010601] [HealthKit] New data: [222 mg/dL 6AA78B15-4EB5-4F80-823E-DFBC43AB3C5A "Salud" (15.5), "iPhone14,2" (15.5)metadata: {
2022-06-10 10:54:06.475530+0200 Cori[81627:9010591] [persistence] HealthKit: Successfully imported data.
2022-06-10 10:54:06.493688+0200 Cori[81627:9010601] [HealthKit] 1 new HKQuantityTypeIdentifierBloodGlucose samples
2022-06-10 10:54:06.493899+0200 Cori[81627:9010591] [persistence] HealthKit: Start import to Core Data
2022-06-10 10:54:06.494013+0200 Cori[81627:9010591] [persistence] HealthKit: 1 samples of type HKQuantityTypeIdentifierBloodGlucose not from this app!
2022-06-10 10:54:06.494057+0200 Cori[81627:9010591] [persistence] HealthKit: Start batch insert request.
2022-06-10 10:54:06.495381+0200 Cori[81627:9010601] [persistence] HealthKit: Successfully imported data.
I'm also having the same issues. Specially when the app is in background phase.