0

I am trying to determine when a query has finished executing. I have implemented a completion callback however, as it is called with each loop I would have to devise some scheme to determine when the query has actually completed. Is there another way?

I am hesitant to block the main thread while the query is executing.

    func updateData(completion: @escaping (Bool) -> () ) {
        .....
        statsCollection.enumerateStatistics(from: startDate, to: endDate as Date)  { [unowned self] statistics, stop in
            if let quantity = statistics.sumQuantity() {
                    DispatchQueue.main.async(execute: {
                        let date = statistics.startDate
                        let value = quantity.doubleValue(for: HKUnit.count())
                        self.setCount(value, forDate: date)
                        completion(true)
                    })
                }
            }
        }

        healthStore.execute(query)
    |
  • Where do you initialize `query`? You can specify a `completionHandler` in its `init`: https://developer.apple.com/documentation/healthkit/hksourcequery/1614367-init – Paolo Jul 26 '17 at 17:40
  • Hello Paolo,, Thank you for your comment. I awn using HKStatisticsCollectionQuery which does not appear to allow a completionHandler: https://developer.apple.com/documentation/healthkit/hkstatisticscollectionquery – John Smith Jul 26 '17 at 19:27
  • I ended up adding a condition - in this case if the enddate is greater than today - then call completion if( statistics.endDate > endDate ){ completion(true) } – John Smith Jul 26 '17 at 20:02

0 Answers0