gpt4 book ai didi

swift - HKWorkout 的 HKSampleQuery

转载 作者:行者123 更新时间:2023-11-28 07:15:58 24 4
gpt4 key购买 nike

我在查询 HKWorkout 时遇到问题。

下面是我用来保存 HKWorkoutHKQuantitySample 的代码。

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)
}

我保存了代码并且工作正常。即使我可以像 HKSampleQuery 一样查询

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_))")
}
};

但是,当我尝试使用 HKWorkoutType 进行查询时,我什么也得不到。

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 {
}
}

奇怪的是我没有收到完成 block 的日志。第 1、2、3 条评论日志中没有日志。

当我使用 class func predicateForObjectsFromWorkout(workout: HKWorkout!) -> NSPredicate! 时,它按预期工作。

为什么我无法通过 HKWorkoutType() 查询获得任何信息?

BKLog只是一个成型的日志函数

最佳答案

这似乎是 iOS8.1 中修复的 HealthKit 错误。我在 8.1 之前遇到了完全相同的问题,但相同的代码在 8.1 中运行良好。我检查使用 8.1 使用:

    NSOperatingSystemVersion iOSVersionInWhichHealthKitActuallyWorks;
iOSVersionInWhichHealthKitActuallyWorks.majorVersion = 8;
iOSVersionInWhichHealthKitActuallyWorks.minorVersion = 1;
iOSVersionInWhichHealthKitActuallyWorks.patchVersion = 0;

if (![[NSProcessInfo processInfo]isOperatingSystemAtLeastVersion:iOSVersionInWhichHealthKitActuallyWorks]) {
}

关于swift - HKWorkout 的 HKSampleQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com