Skip to content

Instantly share code, notes, and snippets.

@cojoj
Created September 15, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cojoj/7a59aedc0e553b8d4523 to your computer and use it in GitHub Desktop.
Save cojoj/7a59aedc0e553b8d4523 to your computer and use it in GitHub Desktop.
func readSample(sampleType: HKSampleType, predicate: NSPredicate?, limit: Int?, completion: ((results: [HKSample]?, error: ErrorType?) -> Void)!) {
// Read stored anchor (If this is the first run 0 will be returned which is equal to HKAnchoredObjectQueryNoAnchor)
let anchorValue = defaults.integerForKey(sampleType.identifier)
let limit = limit ?? Int(HKObjectQueryNoLimit)
let query = HKAnchoredObjectQuery(type: sampleType, predicate: predicate, anchor: anchorValue, limit: limit) { (query, results, newAnchor, error) -> Void in
guard let results = results where error == nil else {
completion(results: nil, error: error)
return
}
// Here I'm 'dynamically' creating new keys
self.defaults.setInteger(newAnchor, forKey: sampleType.identifier)
completion(results: results, error: nil)
}
healthKitStore.executeQuery(query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment