I can't find a way to have the makeQuery() function return the summary result. How do I do this? I want to be able to call makeQuery() and have it return summary. summary is an object holding some data. HKActivitySummary to be specific.
func makeQuery() {
let calendar = NSCalendar.current
let endDate = Date()
guard let startDate = calendar.date(byAdding: .day, value: 0, to: endDate) else {
fatalError("error")
}
let units: Set<Calendar.Component> = [.day, .month, .year, .era]
var startDatecomps = calendar.dateComponents(units, from: startDate)
startDatecomps.calendar = calendar
var endDatecomps = calendar.dateComponents(units, from: endDate)
endDatecomps.calendar = calendar
let summariesWithinRange = HKQuery.predicate(
forActivitySummariesBetweenStart: startDatecomps, end: endDatecomps)
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) {
(sample, results, error) -> Void in
if let results = results {
if let summary = results.first {
print(summary) // how to have makeQuery() return the summary value? Just printing it to the console right now to confirm data is being retrieved
}
}
}
healthstore.execute(query)
}
Related
since this is my first Swift project and I am new to programming in general. I need some help with extracting the sample data I fetched from the query and printing it in the console. I am looking for a way to extract latestHr from the local scope and display it in the View in the app itself which is in the scope below:
var body: some View {
return VStack {
}
but I couldn't find a way to do it correctly.
This is most of the code I am using and the parts for calculation and fetching.
func latestheartRate(){
guard let sampleType = HKObjectType.quantityType(forIdentifier: .heartRate) else{return}
let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) {(sample, result, error) in guard error == nil else {
return
}
let data = result![0] as! HKQuantitySample
let unit = HKUnit(from:"count/min")
let latestHr = data.quantity.doubleValue(for: unit)
let date = DateFormatter()
date.dateFormat = "HH:mm E, d MMM y"
let StartDate = date.string(from: data.startDate)
let EndDate = date.string(from: data.endDate)
print("Latest Hr\(latestHr) BPM // StartDate \(StartDate): EndDate \(EndDate)")
let HR = String(latestHr)
}
healthStore.execute(query)
}
//trigger the function to authorize read and write heart rate
init()
{
authorizeHealthKit()
}
var body: some View {
return VStack {
}
Below is my function which is not changing the return Value. How do I get the function to return the modified returnVal?
func RecieveActivitySummary() -> HKActivitySummary {
var returnVal = HKActivitySummary()
self.makeQuery() { (summary) in
returnVal = summary //attempting to alter returnVal
print(returnVal) //prints out returnVal object that I want with data (seemingly modified)
}
print(returnVal) //prints out unmodified returnVal
return returnVal //then goes on to return an empty returnVal object
}
Here is my makeQuery() function if it helps address the problem:
func makeQuery(completion: #escaping (_ summary : HKActivitySummary) -> ()) {
let calendar = NSCalendar.current
let endDate = Date()
guard let startDate = calendar.date(byAdding: .day, value: 0, to: endDate) else {
fatalError("error")
}
let units: Set<Calendar.Component> = [.day, .month, .year, .era]
var startDatecomps = calendar.dateComponents(units, from: startDate)
startDatecomps.calendar = calendar
var endDatecomps = calendar.dateComponents(units, from: endDate)
endDatecomps.calendar = calendar
let summariesWithinRange = HKQuery.predicate(forActivitySummariesBetweenStart: startDatecomps, end: endDatecomps)
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) {
(sample, results, error) -> Void in
if let results = results {
var summary = results[0]
completion(summary)
}
}
healthstore.execute(query)
}
Below is my query. When I print out the data in my ContentView section (below), accurate data from only the past week is shown and every other day is "nil" when I know for sure there is data for those days even when the predicate should go back 30 days.
func makeElevationQuery(completion: #escaping (HKStatisticsCollection?) -> ()) {
let elevationType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.flightsClimbed)!
let calendar = NSCalendar.current
let endDate = Date()
let startDate = calendar.date(byAdding: .day, value: -30, to: endDate)
let anchorDate = Date.mondayAt12AM()
let daily = DateComponents(day : 1)
let summariesWithinRange = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
elevationQuery = HKStatisticsCollectionQuery(quantityType: elevationType, quantitySamplePredicate: summariesWithinRange, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)
elevationQuery!.initialResultsHandler = {elevationQuery, statisticsCollection, error in
completion(statisticsCollection)
}
healthstore.execute(elevationQuery!)
}
}
extension Date {
static func mondayAt12AM() -> Date {
return Calendar(identifier: .iso8601).date(from: Calendar(identifier: .iso8601).dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))!
}
}
Content View Part:
struct ContentView: View {
#StateObject var fitness = main()
#State var final = HKActivitySummary()
#State var MonthElevation = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.flightsClimbed)
var body: some View {
Button("refresh"){
fitness.authorizeHealthkit()
fitness.makeQuery() { (summary) in
final = summary
fitness.makeElevationQuery { statisticsCollection in
if let statisticsCollection = statisticsCollection {
let startDate = Calendar.current.date(byAdding: .day, value: -30, to: Date())
let endDate = Date()
statisticsCollection.enumerateStatistics(from: startDate!, to: endDate) {
(statistics, stop) in
let count = statistics.sumQuantity()?.doubleValue(for: .count())
print(count) //prints out flights climbed on that day correctly for first week only but just "nil" another 23 times (because I had it go back a month)
}
}
}
}
Are you running this on watchOS or iOS? Sounds like you might be on watchOS since watch only has around a weeks worth of data. See earliestPermittedSampleDate.
How do you parse information received from a query? I am looking to get the active calories burned using the following:
func getActiveEnergy() {
let calendar = NSCalendar.current
let now = Date()
let components = calendar.dateComponents([.year, .month, .day], from: now)
guard let startDate = calendar.date(byAdding: .day, value: -7, to: now) else {
fatalError("*** Unable to create the start date")
}
print("startDate -> \(startDate)")
guard let endDate = calendar.date(from: components) else {
fatalError("*** Unable to create the end date")
}
print("endDate -> \(endDate)")
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
let sort = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)
guard let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned) else {
fatalError("*** This method should never fail ***")
}
let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sort]) { (query, results, error) in
guard let samples = results as? [HKQuantitySample] else {
print("There was an error creating the query")
return
}
for sample in samples {
print("sample from query -> \(sample)")
}
DispatchQueue.main.async {
}
}
self.healthKit.execute(query)
}
The results produce a string of information:
0.055 kcal 0FBBA46F-F9C0-4BB7-87F2-84B567900084 "Kevin’s Series 4" (6.2.1), "Watch5,2" (6.2.1)"Apple Watch" (2020-04-14 17:05:27 -0400 - 2020-04-14 17:05:37 -0400)
I only want the value (double) at the beginning of the string.
Thank you for your assistance.
I am trying to get the difference between dates in days. I have a problem translating dates to the desired format.
func daysBefore(_ date: String) -> Int {
let currentDate = Date()
let dateFormater = DateFormatter()
dateFormater.dateFormat = "dd.mm.YYYY"
guard let nextDate = dateFormater.date(from: date) else {
return 0
}
return currentDate.interval(ofComponent: .day, fromDate: nextDate)
}
extension Date {
func interval(ofComponent comp: Calendar.Component, fromDate date: Date) -> Int {
let currentCalendar = Calendar.current
guard let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 }
guard let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 }
return end - start
}
}
daysBefore("22.09.2019")
When I convert the date from String to Date in:
let currentDate = Date()
let dateFormater = DateFormatter()
dateFormater.dateFormat = "dd.mm.YYYY"
guard let nextDate = dateFormater.date(from: date) else {
return 0
}
I always get something like: 2018-12-22 21:09:00 +0000.
But expected(in example): 22.09.2019.