Created
September 15, 2015 06:19
-
-
Save cojoj/7a59aedc0e553b8d4523 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
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