Tobias Ruano
Tobias Ruano

Reputation: 37

How to obtain Health data from the last 7 days

I'm trying to obtain the steps from the last 7 days, but I could not find how to do it. What i would like to receive is an array of 7 elements in which every element is a Day with it respectives total steps. I currently have this code, which obtains today's steps:

//Gets the steps
func getTodaysSteps(completion: @escaping (Double) -> Void) {
    let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!

    let now = Date()
    let startOfDay = Calendar.current.startOfDay(for: now)
    let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

    let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
        guard let result = result, let sum = result.sumQuantity() else {
            print("Failed to fetch steps = \(error?.localizedDescription ?? "N/A")")
            completion(0.0)
            return
        }

        DispatchQueue.main.async {
            completion(sum.doubleValue(for: HKUnit.count()))
        }
    }
    healthKitStore.execute(query)
}

And I call the function like this:

getTodaysSteps { (steps) in
        self.stepsNumber = Int(steps)
    }

Upvotes: 1

Views: 2854

Answers (3)

Bhanu Birani
Bhanu Birani

Reputation: 1286

There's even more simpler solution here.

func getTotalSteps(forPast days: Int, completion: @escaping (Double) -> Void) {
    // Getting quantityType as stepCount
    guard let stepsQuantityType = HKObjectType.quantityType(forIdentifier: .stepCount) else {
        print("*** Unable to create a step count type ***")
        return
    }

    let now = Date()
    let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: now, options: .strictStartDate)

    let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
        guard let result = result, let sum = result.sumQuantity() else {
            completion(0.0)
            return
        }
        completion(sum.doubleValue(for: HKUnit.count()))
    }
    execute(query)
}

Now to call just use the following code:

HKHealthStore().getTotalSteps(forPast: 30) { totalSteps in
    print(totalSteps)
}

Upvotes: 3

Allan
Allan

Reputation: 7363

Try using HKStatisticsCollectionQuery, which will do the date math for you and bucket the results automatically. Here's an example that should provide the step counts for the last 7 days:

    let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!

    let now = Date()
    let exactlySevenDaysAgo = Calendar.current.date(byAdding: DateComponents(day: -7), to: now)!
    let startOfSevenDaysAgo = Calendar.current.startOfDay(for: exactlySevenDaysAgo)
    let predicate = HKQuery.predicateForSamples(withStart: startOfSevenDaysAgo, end: now, options: .strictStartDate)

    let query = HKStatisticsCollectionQuery.init(quantityType: stepsQuantityType,
                                                 quantitySamplePredicate: predicate,
                                                 options: .cumulativeSum,
                                                 anchorDate: startOfSevenDaysAgo,
                                                 intervalComponents: DateComponents(day: 1))

    query.initialResultsHandler = { query, results, error in
        guard let statsCollection = results else {
            // Perform proper error handling here...
        }

        statsCollection.enumerateStatistics(from: startOfSevenDaysAgo, to: now) { statistics, stop in

            if let quantity = statistics.sumQuantity() {
                let stepValue = quantity.doubleValueForUnit(HKUnit.countUnit())
                // ...
            }
        }
    }

Upvotes: 6

David Pasztor
David Pasztor

Reputation: 54795

The only change you have to implement is to change the Date object you supply as the startWith parameter to your HKStatisticsQuery. You can create a Date object representing the start of the day 7 days ago by first going back exactly 7 days in time using Calendar.date(byAdding:,to:), then calling startOfDay(for:) on that object.

let now = Date()
let exactlySevenDaysAgo = Calendar.current.date(byAdding: DateComponents(day: -7), to: now)!
let startOfSevenDaysAgo = Calendar.current.startOfDay(for: exactlySevenDaysAgo)
let predicate = HKQuery.predicateForSamples(withStart: startOfSevenDaysAgo, end: now, options: .strictStartDate)

Upvotes: 0

Related Questions