HKHealthStore().execute(query) will block main thread

HKHealthStore().execute(query) run in main thread it cost 3.5s

here is my code print("execute begin:(Date().timeIntervalSince1970)") HKHealthStore().execute(query) print("execute end:(Date().timeIntervalSince1970)")

query.predicate like this (startDate >= CAST(0.000000, "NSDate") AND endDate < CAST(0.100000, "NSDate") AND startDate < CAST(0.100000, "NSDate")) OR (startDate >= CAST(60.000000, "NSDate") AND endDate < CAST(60.100000, "NSDate") AND startDate < CAST(60.100000, "NSDate")) OR (startDate >= CAST(120.000000, "NSDate") AND endDate < CAST(120.100000, "NSDate") AND startDate < CAST(120.100000, "NSDate")) OR (startDate >= CAST(180.000000, "NSDate") AND endDate < CAST(180.100000, "NSDate") AND startDate < CAST(180.100000, "NSDate")) OR (startDate >= CAST(240.000000, "NSDate") AND endDate < CAST(240.100000, "NSDate") AND startDate < CAST(240.100000, "NSDate")) OR (startDate >= CAST(300.000000, "NSDate") AND endDate < CAST(300.100000, "NSDate") AND startDate < CAST(300.100000, "NSDate")) OR (startDate >= CAST(360.000000, "NSDate") AND endDate < CAST(360.100000, "NSDate") AND startDate < CAST(360.100000, "NSDate")) OR (startDate >= CAST(420.000000, "NSDate") AND endDate < CAST(420.100000, "NSDate") AND startDate < CAST(420.100000, "NSDate")) OR (startDate >= CAST(480.000000, "NSDate") AND endDate < CAST(480.100000, "NSDate") AND startDate < CAST(480.100000, "NSDate")) OR (startDate >= CAST(540.000000, "NSDate") AND endDate < CAST(540.100000, "NSDate") AND startDate < CAST(540.100000, "NSDate")) OR (startDate >= CAST(600.000000, "NSDate") AND endDate < CAST(600.100000, "NSDate") AND startDate < CAST(600.100000, "NSDate")) OR (startDate >= CAST(660.000000, "NSDate") AND endDate < CAST(660.100000, "NSDate") AND startDate < CAST(660.100000, "NSDate")) OR (startDate >= CAST(720.000000, "NSDate") AND endDate < CAST(720.100000, "NSDate") AND startDate < CAST(720.100000, "NSDate")) OR (startDate >= CAST(780.000000, "NSDate") AND endDate < CAST(780.100000, "NSDate") AND startDate < CAST(780.100000, "NSDate")) OR (startDate >= CAST(840.000000, "NSDate") AND endDate < CAST(840.100000, "NSDate") AND startDate < CAST(840.100000, "NSDate")) OR (startDate >= CAST(900.000000, "NSDate") AND endDate < CAST(900.100000, "NSDate") AND startDate < CAST(900.100000, "NSDate")) OR (startDate >= CAST(960.000000, "NSDate") AND endDate < CAST(960.100000, "NSDate") AND startDate < CAST(960.100000, "NSDate")) OR (startDate >= CAST(1020.000000, "NSDate") AND endDate < CAST(1020.100000, "NSDate") AND startDate < CAST(1020.100000, "NSDate")) OR (startDate >= CAST(1080.000000, "NSDate") AND endDate < CAST(1080.100000, "NSDate") AND startDate < CAST(1080.100000, "NSDate")) OR (startDate >= CAST(1140.000000, "NSDate") AND endDate < CAST(1140.100000, "NSDate") AND startDate < CAST(1140.100000, "NSDate")) OR (startDate >= CAST(1200.000000, "NSDate") AND endDate < CAST(1200.100000, "NSDate") AND startDate < CAST(1200.100000, "NSDate")) OR (startDate >= CAST(1260.000000, "NSDate") AND endDate < CAST(1260.100000, "NSDate") AND startDate < CAST(1260.100000, "NSDate")) OR (startDate >= CAST(1320.000000, "NSDate") AND endDate < CAST(1320.100000, "NSDate") AND startDate < CAST(1320.100000, "NSDate")) OR (startDate >= CAST(1380.000000, "NSDate") AND endDate < CAST(1380.100000, "NSDate") AND startDate < CAST(1380.100000, "NSDate")) OR (startDate >= CAST(1440.000000, "NSDate") AND endDate < CAST(1440.100000, "NSDate") AND startDate < CAST(1440.100000, "NSDate")) OR (startDate >= CAST(1500.000000, "NSDate") AND endDate < CAST(1500.100000, "NSDate") AND startDate < CAST(

Log execute begin:1739958803.531026 execute end:1739958807.0448961

HKHealthStore.execute(_:) isn't supposed to run in the main queue, as described in the API reference:

"HealthKit executes queries asynchronously on a background queue. Most queries automatically stop after they have finished executing. However, long-running queries—such as observer queries and some statistics collection queries—continue to execute in the background."

For more meaningful comment, you might consider providing more context, for example, the relevant code that runs the query and logs the query time. See tips on writing forums posts, if necessary.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@DTS Engineer document say HealthKit executes queries asynchronously on a background queue. but The method execute(_:) should not block the current thread.

code like print("execute begin:(Date().timeIntervalSince1970)") HKHealthStore().execute(query) print("execute end:(Date().timeIntervalSince1970)")

code like

print("execute begin:(Date().timeIntervalSince1970)") 
HKHealthStore().execute(query) 
print("execute end:(Date().timeIntervalSince1970)")

That will be really a surprise and does not happen to me. Do you have a minimal project that demonstrates the issue?

Your post can contain a link to where your test project is hosted, or you can probably include a .zip directly in your post using the forums attachment feature.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

HKHealthStore().execute(query) will block main thread
 
 
Q