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
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.