Created
March 7, 2017 18:46
-
-
Save saraheolson/486734ab9f94cd0a712f0eab3b00cd0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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