Running HealthKit query inside BGTaskScheduler closure

Hi!

Currently I am facing an issue I don't fully understand.

I have to get the steps and distanceWalkingAndRunning and sync it to the backend periodically for which I have registered a BGTaskScheduler and created a request to trigger it every 15 minutes.

I understand that it's just a request to trigger it every 15 minutes, in reality the OS decides when to run it, no earlier than the set time.

For debugging purposes, one can trigger the task manually which I tried and succeeded. Works as expected.

App is running, the task gets registered and scheduled, I pause the app and send the command to trigger the task.

Upon resuming the app, my task gets executed, the HealthKit query goes through and submits the correct values to the backend.

Now to the issue. If I run the app normally, and wait for the task to get executed, for some reason the HealthKit query always returns 0, ergo syncing wrong values to the backend.

It's hard to debug this case, I have all the permission I need to read the data and works properly when run manually.

Either i need a special entitlement or this is a bug or I am missing something (most likely?). I hope to get some help here.

Here is my class for reference

final class BackgroundTaskManager: NSObject {
    private var cancellables = Set<AnyCancellable>()

    func registerTasks() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: BackgroundTaskManager.processingIdentifier, using: nil) { [manager = self] task in
            manager.handle(task: task)
        }
    }

    func cancelAllTasks() {
        BGTaskScheduler.shared.cancelAllTaskRequests()
    }

    func scheduleTasks() {
        scheduleProcessingTask()
    }

    private func scheduleProcessingTask() {
        let request = BGProcessingTaskRequest(identifier: BackgroundTaskManager.processingIdentifier)
        request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
        request.requiresNetworkConnectivity = true

        do {
            try BGTaskScheduler.shared.submit(request)

        } catch {
            print(error.localizedDescription)
        }
    }
}

extension BackgroundTaskManager {
    private static let processingIdentifier = "SOMEIDENTIFIER"
}

extension BackgroundTaskManager {
    public static let shared = BackgroundTaskManager()
}

extension BackgroundTaskManager {
    func handle(task: BGTask) {
        let provider = HealthDataProvider()

        let steps = Future { promise in
            provider.getTodaysSteps { result in
                promise(result)
            }
        }

        let distance = Future { promise in
            provider.getDistance { result in
                promise(result)
            }
        }

        Publishers.Zip(steps, distance)
            .flatMap { steps, distance -> AnyPublisher<HealthPostRequest.Parser.OutputType, Error> in
                let date = Date()
                let healthPostRequest = HealthPostRequest(steps: steps, distance: distance, date: date)

                return API.shared.postHealth(healthPostRequest: healthPostRequest)
                    .eraseToAnyPublisher()
            }
            .sink()
            .store(in: &cancellables)
    }
}
Running HealthKit query inside BGTaskScheduler closure
 
 
Q