Hi everyone,I'm developing a visionOS app using SwiftUI and RealityKit. I'm facing a challenge with fetching step data from HealthKit.

I have integrated HealthKit into my app to track and display step data. On my iPhone, the step data syncs and shows up immediately. However, when I try to fetch and display this data on my Vision Pro app, there's a noticeable delay before the data appears, even though I've given the necessary permissions and synced data through iCloud.

Memory updated Here is your question in a similar format:

Hi everyone,

I'm developing a visionOS app using SwiftUI and RealityKit. I'm facing a challenge with fetching step data from HealthKit.

Scenario:

I have integrated HealthKit into my app to track and display step data. On my iPhone, the step data syncs and shows up immediately. However, when I try to fetch and display this data on my Vision Pro app, there's a noticeable delay before the data appears, even though I've given the necessary permissions and synced data through iCloud.

Question:

Why is there a delay in fetching step data from HealthKit on my Vision Pro app, and how can I reduce this delay to ensure real-time data display?

Additional Information:

I'm using HKStatisticsQuery to fetch the step data for the current day. The authorization for HealthKit is requested successfully. The data shows up after some time, but the initial delay is causing a poor user experience. Here is the relevant code snippet for fetching today's steps:

class HealthManager: ObservableObject { let healthStore = HKHealthStore() @Published var activities: [String: Activity] = [:]

init() {
    let steps = HKQuantityType(.stepCount)
    let healthTypes: Set = [steps]
    
    Task {
        do {
            try await healthStore.requestAuthorization(toShare: [], read: healthTypes)
        } catch {
            print("error fetching health data")
        }
    }
}

func fetchTodaySteps() {
    let steps = HKQuantityType(.stepCount)
    let predicate = HKQuery.predicateForSamples(withStart: .startOfDay, end: Date())
    let query = HKStatisticsQuery(quantityType: steps, quantitySamplePredicate: predicate) { _, result, error in
        guard let quantity = result?.sumQuantity(), error == nil else {
            print("error fetching today's step data")
            return
        }
        let stepCount = quantity.doubleValue(for: .count())
        print("stepCount \(stepCount)")
    }
    healthStore.execute(query)
}

} I'm open to alternative approaches if this method isn't the most efficient for real-time data fetching.

Thanks in advance for any insights or suggestions!

Best,

Siddharth

Answered by DTS Engineer in 798461022

Yeah, the HealthKit store is synchronized to your Vision Pro via iCloud, and the synchronization may take some time. There is no way for developers to speed up the synchronization unfortunately. I suggest that you file a feedback report if the current synchronization pace doesn't fit your use case, and share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Yeah, the HealthKit store is synchronized to your Vision Pro via iCloud, and the synchronization may take some time. There is no way for developers to speed up the synchronization unfortunately. I suggest that you file a feedback report if the current synchronization pace doesn't fit your use case, and share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for the insights on HealthKit synchronization.

Is it possible to get live health data directly from an Apple Watch to a Vision Pro app using iCloud, Bluetooth, or a local network connection, rather than relying on HealthKit synchronization through the iPhone? I'm looking for a way to display real-time step data without the current synchronization delay.

My current setup involves fetching step data from HealthKit, which shows delays due to iCloud synchronization. I'm exploring whether a direct connection to the Apple Watch via Bluetooth or a local network could provide more immediate data access. Any guidance or suggestions on achieving real-time data retrieval from the Apple Watch would be greatly appreciated.

Thanks again!

Best,

Siddharth

Watch Connectivity isn't supported on visionOS (WCSession.isSupported() returns false), and so you can't use it to transfer data between a visionOS app and a watchOS app.

Networking and CloudKit APIs are available on both watchOS and visionOS. If you have a watchOS app, it can grab the data from the HealthKit store on the watch, and use the APIs to send the data to your visionOS app.

CloudKit isn't a real time system though, and so using CloudKit to transfer data is not real time. For more information, see TN3162: Understanding CloudKit throttles.

Networking APIs (like URLSessions) + your own server may give you more flexibility, but apps that do too much networking can encounter system level throttles as well.

I am not quite familiar with Bluetooth and other peer-to-peer technologies. I believe a connection between a visionOS app and an watchOS app isn't possible, but can't say for sure. Maybe folks who know better that area can comment.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi everyone,I'm developing a visionOS app using SwiftUI and RealityKit. I'm facing a challenge with fetching step data from HealthKit.
 
 
Q