Если есть какие-либо читатели, которые столкнулись с этой проблемой для доступа к удаленному серверу: убедитесь, что порт открыт
Я просто решил свою собственную проблему:
func getWeightData(forDay days: Int, completion: @escaping ((_ weight: Double?, _ date: Date?) -> Void)) {
// Getting quantityType as stepCount
guard let bodyMassType = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
print("*** Unable to create a bodyMass type ***")
return
}
let now = Date()
let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)!
var interval = DateComponents()
interval.day = 1
var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
anchorComponents.hour = 0
let anchorDate = Calendar.current.date(from: anchorComponents)!
// Note to myself:: StatisticsQuery!! Nicht Collection! Option .mostRecent. Achtung, unten auch setzen!!
let query = HKStatisticsCollectionQuery(quantityType: bodyMassType,
quantitySamplePredicate: nil,
options: [.mostRecent],
anchorDate: anchorDate,
intervalComponents: interval)
query.initialResultsHandler = { _, results, error in
guard let results = results else {
print("ERROR")
return
}
results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
// hier wieder .mostRecent!
if let sum = statistics.mostRecentQuantity() {
let bodyMassValue = sum.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2)
completion(bodyMassValue, statistics.startDate)
return
}
}
}
healthStore.execute(query)
}
Это загружает данные из последнего n
дни и возвращает bodyMass, а также дату