Search code examples
swifthealthkithksamplequery

HKSampleQuery for HKWorkout


I have a problem to query HKWorkout.

Below is the code I use to save HKWorkout and HKQuantitySample.

func workout(#distance:Double, start: NSTimeInterval, paused:NSTimeInterval, resume:NSTimeInterval, end:NSTimeInterval, calory:Double) -> HKWorkout {

    var event1_     = HKWorkoutEvent(type: HKWorkoutEventType.Pause, date: self.minutesBeforeNow(paused))
    var event2_     = HKWorkoutEvent(type: HKWorkoutEventType.Resume, date: self.minutesBeforeNow(resume))
    var calories_   = HKQuantity(unit: HKUnit.calorieUnit(), doubleValue: calory)
    var distance_   = HKQuantity(unit: HKUnit.meterUnit(), doubleValue: distance)
    var meta_ = [
        "TITLE":"WORKOUT_TEST",
        "TEAM":"TEAM WAG",
        "ID":"\(arc4random()%999)"
    ]

    var workout_ = HKWorkout(
        activityType: HKWorkoutActivityType.Cycling,
        startDate: self.minutesBeforeNow(start),
        endDate: self.minutesBeforeNow(end),
        workoutEvents: [event1_, event2_],
        totalEnergyBurned: calories_,
        totalDistance: distance_,
        metadata: meta_)

    return workout_
}

func quantity(#distance:Double, start:NSTimeInterval, end:NSTimeInterval) -> HKQuantitySample {

    var meta_ = [
        "TITLE":"QUANTITY_SAMPLE_TEST",
        "ID":"\(arc4random()%99)"]
    var sample_ = HKQuantitySample(
        type: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling),
        quantity: HKQuantity(unit: HKUnit.meterUnit(), doubleValue: distance),
        startDate: self.minutesBeforeNow(start),
        endDate: self.minutesBeforeNow(end),
        metadata:meta_)

    return sample_
}

func minutesBeforeNow(min:NSTimeInterval) -> NSDate {
    return NSDate().dateByAddingTimeInterval(-60 * min)
}

I save with the codes and works fine. Even I can query with HKSampleQuery like

var predicate_ = HKQuery.predicateForSamplesWithStartDate(self.minutesBeforeNow(120), endDate: self.minutesBeforeNow(10), options: HKQueryOptions.None)
var query_ = HKSampleQuery(sampleType: HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceCycling),
        predicate: predicate_,
        limit: 10,
        sortDescriptors: nil) { (let sampleQuery, let samples, let error) -> Void in

            BKLog("")
            for sample in samples as [HKQuantitySample]
            {
                var meter_  = sample.quantity.doubleValueForUnit(HKUnit.meterUnit())
                var km_     = meter_ / 1000
                BKLog("\(sample.metadata?.description), \(km_))")
            }
    };

However when I tried to query with HKWorkoutType, I couldn't get anything.

var query_ = HKSampleQuery(
        sampleType: HKSampleType.workoutType(),
        predicate: nil,
        limit: 10,
        sortDescriptors: nil) { (let query, let objects, let error) -> Void in

            BKLog("")  // 1

            if(error != nil)
            {
                BKLog("ERROR : \(error.localizedDescription)")  // 2
                return
            }
            else
            {
                BKLog("WORKOUST : \(objects.count)")  // 3
            }


            for workout in objects {
            }
        }

The weird thing here is I got no log with the completion block. No log from 1, 2, 3 commented log.

When I use class func predicateForObjectsFromWorkout(workout: HKWorkout!) -> NSPredicate!, it worked as I expected.

Why can't I get anything with HKWorkoutType() query?

BKLog is just a formed log function


Solution

  • This appears to be a HealthKit bug fixed in iOS8.1. I had exactly the same problem prior to 8.1 but same code works perfectly in 8.1. I check using for 8.1 using:

        NSOperatingSystemVersion iOSVersionInWhichHealthKitActuallyWorks;
        iOSVersionInWhichHealthKitActuallyWorks.majorVersion = 8;
        iOSVersionInWhichHealthKitActuallyWorks.minorVersion = 1;
        iOSVersionInWhichHealthKitActuallyWorks.patchVersion = 0;
    
        if (![[NSProcessInfo processInfo]isOperatingSystemAtLeastVersion:iOSVersionInWhichHealthKitActuallyWorks]) {
        }