Friday, 15 June 2012

objective c - HKSampleQuery for HKWorkout -



objective c - HKSampleQuery for HKWorkout -

i have problem query hkworkout.

below code utilize save hkworkout , 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_) homecoming 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_) homecoming sample_ } func minutesbeforenow(min:nstimeinterval) -> nsdate { homecoming nsdate().datebyaddingtimeinterval(-60 * min) }

i save codes , works fine. can query 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, allow samples, allow error) -> void in bklog("") sample in samples [hkquantitysample] { var meter_ = sample.quantity.doublevalueforunit(hkunit.meterunit()) var km_ = meter_ / 1000 bklog("\(sample.metadata?.description), \(km_))") } };

however when tried query hkworkouttype, couldn't anything.

var query_ = hksamplequery( sampletype: hksampletype.workouttype(), predicate: nil, limit: 10, sortdescriptors: nil) { (let query, allow objects, allow error) -> void in bklog("") // 1 if(error != nil) { bklog("error : \(error.localizeddescription)") // 2 homecoming } else { bklog("workoust : \(objects.count)") // 3 } workout in objects { } }

the weird thing here got no log completion block. no log 1, 2, 3 commented log.

when utilize class func predicateforobjectsfromworkout(workout: hkworkout!) -> nspredicate!, worked expected.

why can't hkworkouttype() query?

bklog formed log function

this appears healthkit bug fixed in ios8.1. had same problem prior 8.1 same code works in 8.1. check using 8.1 using:

nsoperatingsystemversion iosversioninwhichhealthkitactuallyworks; iosversioninwhichhealthkitactuallyworks.majorversion = 8; iosversioninwhichhealthkitactuallyworks.minorversion = 1; iosversioninwhichhealthkitactuallyworks.patchversion = 0; if (![[nsprocessinfo processinfo]isoperatingsystematleastversion:iosversioninwhichhealthkitactuallyworks]) { }

objective-c swift health-kit

No comments:

Post a Comment