I am receiving the following crash when trying to query data from health kit.
It seems to be complaining about end date being before start date however with the values I am passing in that is not possible.
Also I tried to purposefully pass in end dates before start date and I still was not able to reproduce this crash.
I have attached my code snippet and the crash logs here.
Please Help :(
self.statisticfetcher(
type: HKObjectType.quantityType(forIdentifier: .bodyMass),
options: .discreteAverage,
metricType: .bodyWeight,
statsHandler:
{ stats in
if let quantity = stats.averageQuantity() {
...
}
return nil
})
private func statisticfetcher(
type: HKQuantityType?,
options: HKStatisticsOptions,
metricType: HealthMetricType,
statsHandler: @escaping (HKStatistics) -> BASchema.HealthDataEntryInput?,
resultHandler: ((HKStatisticsCollection?, Date, Date, Error?) -> Void)? = nil)
{
let res = getStartEndDate()
let endDate = res.endDate
let startDate = res.startDate
guard let type = type else {
return
}
let query = HKStatisticsCollectionQuery(quantityType: type,
quantitySamplePredicate: nil,
options: options,
anchorDate: startDate,
intervalComponents: DateComponents(day: 1))
let defaultResultHandler: (HKStatisticsCollection?, Date, Date, Error?) -> Void = { statCollection, startDate, endDate, error in
if let error = error {
return
}
guard let statCollection = statCollection else {
return
}
statCollection.enumerateStatistics(from: startDate, to: endDate) { stats, _ in
...
}
}
let resultHandler = resultHandler ?? defaultResultHandler
query.initialResultsHandler = { _, statCollection, error in
resultHandler(statCollection, startDate, endDate, error)
}
query.statisticsUpdateHandler = { _, _, statCollection, error in
resultHandler(statCollection, startDate, endDate, error)
}
self.store.execute(query)
self.subscribeToBackgroundDelivery(type: type)
}
private func subscribeToBackgroundDelivery(type: HKObjectType) {
store.enableBackgroundDelivery(for: type, frequency: .immediate) { success, error in
if(error != nil && !success) { print("Background Delivery for \(type) Failed!") }
}
}```
Fatal Exception: NSInternalInconsistencyException
0 CoreFoundation 0x9cb4 __exceptionPreprocess
1 libobjc.A.dylib 0x183d0 objc_exception_throw
2 Foundation 0x4e156c _userInfoForFileAndLine
3 HealthKit 0xe0d2c -[HKDateInterval initWithStartDate:endDate:]
4 HealthKit 0xc7a5c -[NSDateComponents(HealthKit) hk_dateIntervalForDate:anchorDate:outIndex:]
5 HealthKit 0x1a0f98 -[HKStatisticsCollection _statisticsDateIntervalAndIndex:forDate:]
6 HealthKit 0x1a0420 -[HKStatisticsCollection _insertStatistics:]
7 HealthKit 0x1a059c -[HKStatisticsCollection _resetStatistics:]
8 HealthKit 0x19efc8 -[HKStatisticsCollectionQuery _queue_deliverResetStatisticsObjects:forQuery:]
9 HealthKit 0x19e8bc __98-[HKStatisticsCollectionQuery client_deliverStatisticsBatch:resetStatistics:isFinal:anchor:query:]_block_invoke
10 libdispatch.dylib 0x2320 _dispatch_call_block_and_release
11 libdispatch.dylib 0x3eac _dispatch_client_callout
12 libdispatch.dylib 0xb534 _dispatch_lane_serial_drain
13 libdispatch.dylib 0xc0d8 _dispatch_lane_invoke
14 libdispatch.dylib 0x16cdc _dispatch_workloop_worker_thread
15 libsystem_pthread.dylib 0xddc _pthread_wqthread
16 libsystem_pthread.dylib 0xb7c start_wqthread