Skip to content

Instantly share code, notes, and snippets.

@saraheolson
Created March 7, 2017 18:46
Show Gist options
  • Save saraheolson/486734ab9f94cd0a712f0eab3b00cd0a to your computer and use it in GitHub Desktop.
Save saraheolson/486734ab9f94cd0a712f0eab3b00cd0a to your computer and use it in GitHub Desktop.
var anchor: HKQueryAnchor?
func createHeartRateStreamingQuery(_ workoutStartDate: Date) -> HKQuery? {
guard let heartRateType: HKQuantityType = HKQuantityType.quantityType(forIdentifier: .heartRate) else {
print("Could not get heart rate type")
return nil
}
let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate,
end: nil,
options: .strictEndDate)
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [datePredicate])
let heartRateQuery = HKAnchoredObjectQuery(type: heartRateType,
predicate: compoundPredicate,
anchor: nil,
limit: Int(HKObjectQueryNoLimit))
{ (query, sampleObjects, deletedObjects, newAnchor, error) in
guard let newAnchor = newAnchor,
let sampleObjects = sampleObjects else {
return
}
self.anchor = newAnchor
}
heartRateQuery.updateHandler = {(query, sampleObjects, deletedObjects, newAnchor, error) -> Void in
guard let newAnchor = newAnchor,
let sampleObjects = sampleObjects else {
return
}
self.anchor = newAnchor
}
return heartRateQuery
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment