'When I am trying to get all HealthKit data it’s crashing

private var data: [HealthKitRecord] = []
    func syncData() {
            self.data = []
            guard HKHealthStore.isHealthDataAvailable() else { return }
            let dispatchSemaphore = DispatchSemaphore(value: 0)
            readHeartRate(semaphore: dispatchSemaphore)
            readHeartRateMax(semaphore: dispatchSemaphore)
            readHeartRateMin(semaphore: dispatchSemaphore)
            readRestingHeartRate(semaphore: dispatchSemaphore)
            readHeartRateVariability(semaphore: dispatchSemaphore)
            readHeight(semaphore: dispatchSemaphore)
            readBodyMass(semaphore: dispatchSemaphore)
            readMenstrualCycle(semaphore: dispatchSemaphore)
            readMindfulMinutes(semaphore: dispatchSemaphore)
            readStepCount(semaphore: dispatchSemaphore)
            readSleepTime(semaphore: dispatchSemaphore)
            readExerciseMinutes(semaphore: dispatchSemaphore)
            readStandMinutes(semaphore: dispatchSemaphore)
            readActiveBurned(semaphore: dispatchSemaphore)
            readWorkouts(semaphore: dispatchSemaphore)
            bloodPressure(semaphore: dispatchSemaphore)
            readMaxBodyTemperature(semaphore: dispatchSemaphore)
            readAvgBodyTemperature(semaphore: dispatchSemaphore)
            readMinBodyTemperature(semaphore: dispatchSemaphore)
            readMaxiumVO2(semaphore: dispatchSemaphore)
            readMaxOxygenSaturation(semaphore: dispatchSemaphore)
            readAvgOxygenSaturation(semaphore: dispatchSemaphore)
            readMinOxygenSaturation(semaphore: dispatchSemaphore)
            readMaximumRespiratoryRate(semaphore: dispatchSemaphore)
            readAverageRespiratoryRate(semaphore: dispatchSemaphore)
            readMinimumRespiratoryRate(semaphore: dispatchSemaphore)
            readMaximumBloodGlucose(semaphore: dispatchSemaphore)
            readAverageBloodGlucose(semaphore: dispatchSemaphore)
            readMinimumBloodGlucose(semaphore: dispatchSemaphore)
            readMaximumBodyFatPercentageRate(semaphore: dispatchSemaphore)
            readAverageBodyFatPercentageRate(semaphore: dispatchSemaphore)
            readMinimumBodyFatPercentageRate(semaphore: dispatchSemaphore)
            readWheelChairPushCount(semaphore: dispatchSemaphore)
            readdistanceWheelchair(semaphore: dispatchSemaphore)
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            dispatchSemaphore.wait()
            self.uploadData()
        }

I am getting this error:

    malloc: *** set a breakpoint in malloc_error_break to debug
    dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib

How to solve my issue or I have no idea? Anyone can help me what's i am doing wrong.

I am also tried this approach but it's not reading data from apple healthkit.

private func addHealthRecord(
        statisticsCollectionQuery: HKStatisticsCollectionQuery?,
        unit: HKUnit,
        quantity: @escaping (_ statistics: HKStatistics) -> HKQuantity?,
        healthKitParamData: @escaping (_ quantityDouble: Double) -> HealthKitParamData,
        completion:  @escaping  () -> Void
    ) {
        guard let statisticsCollectionQuery = statisticsCollectionQuery else {
            return completion()
        }
        statisticsCollectionQuery.initialResultsHandler = { [weak self] _, statisticsCollection, _ in
            guard let self = self,
                  let statisticsCollection = statisticsCollection else {
                      return completion()
                  }
            for statistic in statisticsCollection.statistics() {
                guard let quantity = quantity(statistic) else {
                    continue
                }
                let quantityDouble = quantity.doubleValue(for: unit)
                let dateString = dateFormatHelper(fromDate: statistic.startDate)
                var isFound = false
                for (index, record) in self.healthKitRecords.enumerated() where record.date == dateString {
                    isFound = true
                    self.healthKitRecords[index].data = healthKitParamData(quantityDouble)
                    break
                }
                if !isFound {
                    let healthKitRecord = HealthKitRecord(
                        type: "Healthkit",
                        segment: "",
                        date: dateString,
                        uid: "",
                        data: healthKitParamData(quantityDouble)
                    )
                    self.healthKitRecords.append(healthKitRecord)
                }
            }
            completion()
        }
        HKHealthStore().execute(statisticsCollectionQuery)
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source