swift - HealthKit fetch data between interval -


i have little problem grasping healthkit. want heart rate healthkit specific time. have done in past (until noticed couldn't fetch data when phone locked)

 func retrievemostrecentheartratesample(completionhandler: (sample: hkquantitysample) -> void) {     let sampletype = hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierheartrate)     let predicate = hkquery.predicateforsampleswithstartdate(nsdate.distantpast() as! nsdate, enddate: nsdate(), options: hkqueryoptions.none)     let sortdescriptor = nssortdescriptor(key: hksamplesortidentifierstartdate, ascending: false)      let query = hksamplequery(sampletype: sampletype, predicate: predicate, limit: 1, sortdescriptors: [sortdescriptor])         { (query, results, error) in             if error != nil {                 println("an error has occured following description: \(error.localizeddescription)")             } else {                 let mostrecentsample = results[0] as! hkquantitysample                 completionhandler(sample: mostrecentsample)             }         }     healthkitstore.executequery(query) }  var observequery: hkobserverquery!  func startobservingforheartratesamples() {     println("startobservingforheartratesamples")     let sampletype = hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierheartrate)      if observequery != nil {         healthkitstore.stopquery(observequery)     }      observequery = hkobserverquery(sampletype: sampletype, predicate: nil) {         (query, completionhandler, error) in          if error != nil {             println("an error has occured following description: \(error.localizeddescription)")         } else {            self.retrievemostrecentheartratesample {                 (sample) in                 dispatch_async(dispatch_get_main_queue()) {                 let result = sample                 let quantity = result.quantity                 let count = quantity.doublevalueforunit(hkunit(fromstring: "count/min"))                 println("sample: \(count)")                 heartchartdelegate?.updatechartwith(count)                 }             }         }     }     healthkitstore.executequery(observequery) } 

this code fetch latest sample every time change in healthkit. said earlier, won't update when phone locked. tried using:

self.healthkitstore.enablebackgrounddeliveryfortype(hkquantitytype.quantitytypeforidentifier(hkquantitytypeidentifierheartrate), frequency: hkupdatefrequency.immediate) { (success, error) in if success{    println("success") } else {    println("fail") }             

}

but didn't work , found out there bug apple said wasn't working wanted. guess security-thing.

but thought, maybe can request samples between starttime , endtime. example have endtime(2015-05-31 10:34:45 +0000) , starttime(2015-05-31 10:34:35 +0000). question how can heart rate samples between these 2 times.

i guess must in the

hkquery.predicateforsampleswithstartdate(mystarttime, enddate: myendtime, options: hkqueryoptions.none) 

but when tried didn't find anything. maybe got wrong...

i using heart rate monitor on chest , know values in healthkit within start , end time.

edit:

ok tried , working times, not always. has idea?

func fetchheartrates(endtime: nsdate, starttime: nsdate){     let sampletype = hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierheartrate)     let predicate = hkquery.predicateforsampleswithstartdate(starttime, enddate: endtime, options: hkqueryoptions.none)     let sortdescriptor = nssortdescriptor(key: hksamplesortidentifierstartdate, ascending: false)      let query = hksamplequery(sampletype: sampletype, predicate: predicate, limit: 100, sortdescriptors: [sortdescriptor])         { (query, results, error) in             if error != nil {                 println("an error has occured following description: \(error.localizeddescription)")             } else {                 r in results{                     let result = r as! hkquantitysample                     let quantity = result.quantity                     let count = quantity.doublevalueforunit(hkunit(fromstring: "count/min"))                     println("sample: \(count) : \(result)")                 }             }     }     healthkitstore.executequery(query) } 

edit 2:

it working couldn't call way did. fetched couple of seconds later , worked fine :)

...but said earlier, won't update when phone locked...guess security-thing.

you correct.

from healthkit framework reference:

because healthkit store encrypted, your app cannot read data store when phone locked. means app may not able access store when launched in background. however, apps can still write data store, when phone locked. store temporarily caches data , saves encrypted store phone unlocked.

if want app alerted when there new results, should review managing background delivery:

enablebackgrounddeliveryfortype:frequency:withcompletion:

call method register app background updates. healthkit wakes app whenever new samples of specified type saved store. app called @ once per time period defined specified frequency.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -