For iOS healthkit how to save systolic and diastolic blood pressure values? - iphone

Here is code for saving blood pressure data in health kit
HKUnit *BPunit = [HKUnit millimeterOfMercuryUnit];
HKQuantity *BPSysQuantity = [HKQuantity quantityWithUnit:BPunit doubleValue:150.0];
HKQuantityType *BPSysType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
HKQuantitySample *BPSysSample = [HKQuantitySample quantitySampleWithType:BPSysType quantity:BpsysQuantity startDate:now endDate:now];
[self.healthStore saveObject:BPSysSample withCompletion:^(BOOL success, NSError *error)
same way for diastolic also,
But how to save both combine as single entry in health app? Currently two diffrent entries are saved for systolic and diastolic blood pressure in health app.

- (void)saveBloodPressureIntoHealthStore:(double)Systolic Dysbp:(double)Diastolic {
HKUnit *BloodPressureUnit = [HKUnit millimeterOfMercuryUnit];
HKQuantity *SystolicQuantity = [HKQuantity quantityWithUnit:BloodPressureUnit doubleValue:Systolic];
HKQuantity *DiastolicQuantity = [HKQuantity quantityWithUnit:BloodPressureUnit doubleValue:Diastolic];
HKQuantityType *SystolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic];
HKQuantityType *DiastolicType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic];
NSDate *now = [NSDate date];
HKQuantitySample *SystolicSample = [HKQuantitySample quantitySampleWithType:SystolicType quantity:SystolicQuantity startDate:now endDate:now];
HKQuantitySample *DiastolicSample = [HKQuantitySample quantitySampleWithType:DiastolicType quantity:DiastolicQuantity startDate:now endDate:now];
NSSet *objects=[NSSet setWithObjects:SystolicSample,DiastolicSample, nil];
HKCorrelationType *bloodPressureType = [HKObjectType correlationTypeForIdentifier:
HKCorrelationTypeIdentifierBloodPressure];
HKCorrelation *BloodPressure = [HKCorrelation correlationWithType:bloodPressureType startDate:now endDate:now objects:objects];
[self.healthStore saveObject:BloodPressure withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(#"An error occured saving the height sample %#. In your app, try to handle this gracefully. The error was: %#.", BloodPressure, error);
abort();
}
[_activity stopAnimating];
UIAlertView *savealert=[[UIAlertView alloc]initWithTitle:#"HealthDemo" message:#"Blood Pressure values has been saved to Health App" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[savealert show];
}];
}

In Swift 3:
func saveBloodPressure(systolic systolicValue: Double, diastolic diastolicValue: Double, completion completionBlock: #escaping (Bool, Error?) -> Void) {
let unit = HKUnit.millimeterOfMercury()
let systolicQuantity = HKQuantity(unit: unit, doubleValue: systolicValue)
let diastolicQuantity = HKQuantity(unit: unit, doubleValue: diastolicValue)
let systolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic)!
let diastolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic)!
let nowDate = Date()
let systolicSample = HKQuantitySample(type: systolicType, quantity: systolicQuantity, start: nowDate, end: nowDate)
let diastolicSample = HKQuantitySample(type: diastolicType, quantity: diastolicQuantity, start: nowDate, end: nowDate)
let objects: Set<HKSample> = [systolicSample, diastolicSample]
let type = HKObjectType.correlationType(forIdentifier: .bloodPressure)!
let correlation = HKCorrelation(type: type, start: nowDate, end: nowDate, objects: objects)
self.healthKitStore.save(correlation) { (success, error) -> Void in
if !success {
print("An error occured saving the Blood pressure sample \(systolicSample). In your app, try to handle this gracefully. The error was: \(error).")
}
completionBlock(success, error)
}
}

Check out HKCorrelation. A correlation is a set of related objects and is designed to represent things like blood pressure readings and food. You can save create and save correlations just like samples and you can query for correlations using HKCorrelationQuery.

Swift : iOS : Save Blood Pressure:
private func saveBloodPressureIntoHealthStore(bloodPressureValueSystolic:Double
,bloodPressureValueDiastolic:Double) -> Void {
// Save the user's blood pressure into HealthKit.
let bloodPressureUnit: HKUnit = HKUnit.millimeterOfMercuryUnit()
let bloodPressureSystolicQuantity: HKQuantity = HKQuantity(unit: bloodPressureUnit, doubleValue: bloodPressureValueSystolic)
let bloodPressureDiastolicQuantity: HKQuantity = HKQuantity(unit: bloodPressureUnit, doubleValue: bloodPressureValueDiastolic)
let bloodPressureSystolicType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic)
let bloodPressureDiastolicType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)
let nowDate: NSDate = NSDate()
let bloodPressureSystolicSample: HKQuantitySample = HKQuantitySample(type: bloodPressureSystolicType
, quantity: bloodPressureSystolicQuantity, startDate: nowDate, endDate: nowDate)
let bloodPressureDiastolicSample: HKQuantitySample = HKQuantitySample(type: bloodPressureDiastolicType
, quantity: bloodPressureDiastolicQuantity, startDate: nowDate, endDate: nowDate)
let completion: ((Bool, NSError!) -> Void) = {
(success, error) -> Void in
if !success {
println("An error occured saving the Blood pressure sample \(bloodPressureSystolicSample). In your app, try to handle this gracefully. The error was: \(error).")
abort()
}
}// end completion
var objects : NSSet = NSSet(objects: bloodPressureSystolicSample,bloodPressureDiastolicSample)
var bloodPressureType: HKCorrelationType = HKObjectType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierBloodPressure)
var bloodPressureCorrelation : HKCorrelation = HKCorrelation(type: bloodPressureType, startDate: nowDate
, endDate: nowDate, objects: objects)
self.healthStore!.saveObject(bloodPressureCorrelation, withCompletion: completion)
}// end saveBloodPressureIntoHealthStore

Xamarin.iOS solution
public void SaveBloodPressure(DateTime date, double systolic, double diastolic, double beatsPerMinute)
{
using (var healthKitStore = new HKHealthStore())
{
var heartRateUnitType = HKUnit.MillimeterOfMercury;
var diastolicQuantity = HKQuantity.FromQuantity(heartRateUnitType, diastolic);
var diastolicQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.BloodPressureDiastolic);
var diastolicSample = HKQuantitySample.FromType(diastolicQuantityType, diastolicQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata());
var systolicQuantity = HKQuantity.FromQuantity(heartRateUnitType, systolic);
var systolicQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.BloodPressureSystolic);
var systolicSample = HKQuantitySample.FromType(systolicQuantityType, systolicQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata());
var objects = new NSSet(systolicSample, diastolicSample);
var bloodPressureType = HKCorrelationType.GetCorrelationType(HKCorrelationTypeKey.IdentifierBloodPressure);
var bloodPressure = HKCorrelation.Create(bloodPressureType, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), objects);
try
{
healthKitStore.SaveObject(bloodPressure, (success, error) =>
{
//action to take on success/failure
});
}
catch (Exception)
{
//handle exception
}
try
{
var beatsPerMinuteUnits = HKUnit.Count.UnitDividedBy(HKUnit.Minute);
var beatsPerMinuteQuantity = HKQuantity.FromQuantity(beatsPerMinuteUnits, beatsPerMinute);
var heartRateQuantityType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.HeartRate);
var heartRateSample = HKQuantitySample.FromType(heartRateQuantityType, beatsPerMinuteQuantity, date.ToUniversalTime().ToNSDate(), date.ToUniversalTime().ToNSDate(), new HKMetadata());
healthKitStore.SaveObject(heartRateSample, (success, error) =>
{
//handle success / failure
});
}
catch (Exception)
{
//handle exception
}
}
}

Related

Fetched data from private CloudKit database not returning from function

I'm trying to fetch some data from a private ClouKit database. The query and predicate is working fine because I can see at print(data) // 1 that the array is filled with all the data. But as soon as it leaves perform(){} the array is resetted to it's at the top defined values and it just returns them.
func fetchUserRecord() -> [Double] {
var data: [Double] = [0, 1, 2] //demo data
let aWeekAgo = Date().addingTimeInterval(-604800)
let privateDB = CKContainer.default().privateCloudDatabase
let predicate = NSPredicate(format: "Date > %#", aWeekAgo as NSDate)
let query = CKQuery(recordType: "ProgressionValue", predicate: predicate)
privateDB.perform(query, inZoneWith: nil) { records, error in
guard let records = records else { return }
for record in records {
data.append(record["Value"] as! Double)
}
data.append(0.0)
print(data) // 1
}
print(data) // 2
return data
}
Here are the outputs:
print(data) //1:
[0.0, 1.0, 2.0, {tons of doubles}, 0.0]
print(data) //2:
[0.0, 1.0, 2.0]
What am I missing?
Try the new synch tools;
func fetchUserRecord() async throws -> [Double]
{
var data: [Double] = [0, 1, 2] // you don't really want the
let aWeekAgo = Date().addingTimeInterval(-604800)
let privateDB = CKContainer.default().privateCloudDatabase
let predicate = NSPredicate(format: "Date > %#", aWeekAgo as NSDate)
let query = CKQuery(recordType: "ProgressionValue", predicate: predicate)
let (values, cursor) = try await privateDB.records(matching: query, resultsLimit: 100)
for r in values
{
if let rec = try? r.1.get()
{
data.append(rec["value"] as Double)
}
}
return x
}
Call it like this;
Task
{
do {
data = try await fetchUserRecord()
}
catch
{
print(error)
}
}

EKEvent event.eventIdentifier not removing

I am trying to remove an event I saved in the calendar with an event id but its removing a different event and sometimes it doesn't remove anything.
I am saving the eventId in a realm database when event is created and reading it back when I want to delete but its not working.
I have tried running it on an actual device, using an array instead of a dictionary, changing the span to .futureEvents but still doesn't work
my code for creating event and saving to realm database
/// function exits in another class
func addEventToCalendar(userName: String, userDate: Date) {
let userDefaults = UserDefaults.standard
let eventStore: EKEventStore = EKEventStore()
eventStore.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(String(describing: error))")
let event: EKEvent = EKEvent(eventStore: eventStore)
event.title = "\(userName) Birthday"
event.startDate = userDate
event.endDate = userDate
event.notes = "Happy Birthday!"
event.isAllDay = true
event.calendar = eventStore.defaultCalendarForNewEvents
let ekrules: EKRecurrenceRule = EKRecurrenceRule.init(recurrenceWith: .yearly, interval: 1, end: nil)
event.recurrenceRules = [ekrules]
//event.addAlarm(EKAlarm(absoluteDate: event.startDate))
//sets alert 00:00 on day of event
event.addAlarm(EKAlarm(relativeOffset: 0))
do {
try eventStore.save(event, span: .thisEvent, commit: true)
} catch let error as NSError {
print("error: \(error)")
}
let eventId = event.eventIdentifier ?? "nil-id"
userDefaults.setValue(eventId, forKey: "eventId")
print(eventId)
} else {
print("error not granted: \(String(describing: error))")
}
}
}
//saving it in a view controller class
#IBAction func okBtnPressed(_ sender: UIButton) {
let eventId = UserDefaults.standard.string(forKey: "eventId") ?? "no-id"
//// saving data to device
let newItem = Item()
newItem.userImageName = String(describing: userImageUrl)
newItem.userName = uName
newItem.isYearPresent = uYearPresent
newItem.userDOB = uDOB
newItem.color = UIColor.init(randomFlatColorOf: .dark).hexValue()
newItem.daysRemaining = daysRemain
newItem.eventId = eventId
self.save(item: newItem)
}
The event id saves succesfully in the realm database.
function for removing the event from calendar
func removeEvent(id: String) {
let store = EKEventStore()
store.requestAccess(to: .event) { (granted, error) in
if !granted { return }
// checking if event exists
if let eventToRemove = store.event(withIdentifier: id) {
do {
print("removing: \(id)")
try store.remove(eventToRemove, span: .thisEvent, commit: true)
print("event removed sucessfully")
} catch let error as NSError {
print("error: \(error)")
}
} else {
print("event doesnt exist.")
}
}
}
This is how I remove it
var eventIDS = [Int: String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// inserting evenIds to dictionary to access it in remove function
// I used an array but it gave me the same problem
if let item = itemsObject?[indexPath.row] {
eventIDS[indexPath.row] = item.eventId
}
}
// then I call remove function when swipe taps on cell
removeEvent(id: self.eventIDS[indexPath.row] ?? "")
Sometimes I get event removed successfully but it removes a different
event, sometimes I get the following errors
Error getting event with identifier 2BD633CA-BBEA-47CD-8410-40BCE6362A5C:98D9EAF2-D5EF-420F-B769-7F02B7795E54: Error Domain=EKCADErrorDomain Code=1010 "(null)"
event doesnt exist.
I figured it out. Because eventStore.requestAccess(to: .event) is asynchronous, I was saving the event id in the database before the id existed.
So I had to declare the function to accept a completion handler and return the value inside the completion handler.
//// adding events to calendar
func addEventToCalendar(userName: String, userDate: Date, completion: #escaping (String?)->()) {
let userDefaults = UserDefaults.standard
var eventId = ""
let eventStore: EKEventStore = EKEventStore()
eventStore.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(String(describing: error))")
let event: EKEvent = EKEvent(eventStore: eventStore)
event.title = "\(userName) \(NSLocalizedString("birthday", comment: "birthday"))"
event.startDate = userDate
event.endDate = userDate
event.notes = NSLocalizedString("happyBirthday", comment: "happyBirthday")
event.isAllDay = true
event.calendar = eventStore.defaultCalendarForNewEvents
let ekrules: EKRecurrenceRule = EKRecurrenceRule.init(recurrenceWith: .yearly, interval: 1, end: nil)
event.recurrenceRules = [ekrules]
//event.addAlarm(EKAlarm(absoluteDate: event.startDate))
//sets alert 00:00 on day of event
event.addAlarm(EKAlarm(relativeOffset: 0))
do {
try eventStore.save(event, span: .futureEvents, commit: true)
eventId = event.eventIdentifier ?? "no-Id"
print("Event has been saved with id \(String(describing: eventId))")
userDefaults.setValue(eventId, forKey: "eventId")
} catch let error as NSError {
print("error: \(error)")
}
completion(eventId)
} else {
print("error not granted: \(String(describing: error))")
completion(nil)
}
}
}
and then use it like so
addEventToCalendar(userName: uName, userDate: uDate) { (eventIdentifier) in
if let eventId = eventIdentifier {
print("Event add birthday id \(eventId)")
//// saving data to device
// run on main thread to avoid 'RLMException', reason: 'Realm accessed from incorrect thread.'
DispatchQueue.main.async {
let newItem = Item()
newItem.userImageName = String(describing: self.userImageUrl)
newItem.userName = uName
newItem.isYearPresent = uYearPresent
newItem.userDOB = uDOB
newItem.color = UIColor.init(randomFlatColorOf: .dark).hexValue()
newItem.daysRemaining = daysRemain
newItem.eventId = eventId
self.save(item: newItem)
// review app
self.review()
}

Converting AVAudioPCMBuffer to another AVAudioPCMBuffer

I am trying to convert a determined AVAudioPCMBuffer (44.1khz, 1ch, float32, not interleaved) to another AVAudioPCMBuffer (16khz, 1ch, int16, not interleaved) using AVAudioConverter and write it using AVAudioFile.
My code uses the library AudioKit together with the tap AKLazyTap to get a buffer each determined time, based on this source:
https://github.com/AudioKit/AudioKit/tree/master/AudioKit/Common/Taps/Lazy%20Tap
Here is my implementation:
lazy var downAudioFormat: AVAudioFormat = {
let avAudioChannelLayout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Mono)!
return AVAudioFormat(
commonFormat: .pcmFormatInt16,
sampleRate: 16000,
interleaved: false,
channelLayout: avAudioChannelLayout)
}()
//...
AKSettings.sampleRate = 44100
AKSettings.numberOfChannels = AVAudioChannelCount(1)
AKSettings.ioBufferDuration = 0.002
AKSettings.defaultToSpeaker = true
//...
let mic = AKMicrophone()
let originalAudioFormat: AVAudioFormat = mic.avAudioNode.outputFormat(forBus: 0) //41.100, 1ch, float32...
let inputFrameCapacity = AVAudioFrameCount(1024)
//I don't think this is correct, the audio is getting chopped...
//How to calculate it correctly?
let outputFrameCapacity = AVAudioFrameCount(512)
guard let inputBuffer = AVAudioPCMBuffer(
pcmFormat: originalAudioFormat,
frameCapacity: inputFrameCapacity) else {
fatalError()
}
// Your timer should fire equal to or faster than your buffer duration
bufferTimer = Timer.scheduledTimer(
withTimeInterval: AKSettings.ioBufferDuration/2,
repeats: true) { [weak self] _ in
guard let unwrappedSelf = self else {
return
}
unwrappedSelf.lazyTap?.fillNextBuffer(inputBuffer, timeStamp: nil)
// This is important, since we're polling for samples, sometimes
//it's empty, and sometimes it will be double what it was the last call.
if inputBuffer.frameLength == 0 {
return
}
//This converter is only create once, as the AVAudioFile. Ignore this code I call a function instead.
let converter = AVAudioConverter(from: originalAudioFormat, to: downAudioFormat)
converter.sampleRateConverterAlgorithm = AVSampleRateConverterAlgorithm_Normal
converter.sampleRateConverterQuality = .min
converter.bitRateStrategy = AVAudioBitRateStrategy_Constant
guard let outputBuffer = AVAudioPCMBuffer(
pcmFormat: converter.outputFormat,
frameCapacity: outputFrameCapacity) else {
print("Failed to create new buffer")
return
}
let inputBlock: AVAudioConverterInputBlock = { inNumPackets, outStatus in
outStatus.pointee = AVAudioConverterInputStatus.haveData
return inputBuffer
}
var error: NSError?
let status: AVAudioConverterOutputStatus = converter.convert(
to: outputBuffer,
error: &error,
withInputFrom: inputBlock)
switch status {
case .error:
if let unwrappedError: NSError = error {
print(unwrappedError)
}
return
default: break
}
//Only created once, instead of this code my code uses a function to verify if the AVAudioFile has been created, ignore it.
outputAVAudioFile = try AVAudioFile(
forWriting: unwrappedCacheFilePath,
settings: format.settings,
commonFormat: format.commonFormat,
interleaved: false)
do {
try outputAVAudioFile?.write(from: avAudioPCMBuffer)
} catch {
print(error)
}
}
(Please note that AVAudioConverter and AVAudioFile are being reused, the initialization there doesn't represent the real implementation on my code, just to simplify and make it more simple to understand.)
With frameCapacity on the outputBuffer: AVAudioPCMBuffer set to 512, the audio get chopped. Is there any way to discovery the correct frameCapacity for this buffer?
Written using Swift 4 and AudioKit 4.1.
Many thanks!
I managed to solve this problem installing a Tap on the inputNode like this:
lazy var downAudioFormat: AVAudioFormat = {
let avAudioChannelLayout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Mono)!
return AVAudioFormat(
commonFormat: .pcmFormatInt16,
sampleRate: SAMPLE_RATE,
interleaved: true,
channelLayout: avAudioChannelLayout)
}()
private func addBufferListener(_ avAudioNode: AVAudioNode) {
let originalAudioFormat: AVAudioFormat = avAudioNode.inputFormat(forBus: 0)
let downSampleRate: Double = downAudioFormat.sampleRate
let ratio: Float = Float(originalAudioFormat.sampleRate)/Float(downSampleRate)
let converter: AVAudioConverter = buildConverter(originalAudioFormat)
avAudioNode.installTap(
onBus: 0,
bufferSize: AVAudioFrameCount(downSampleRate * 2),
format: originalAudioFormat,
block: { (buffer: AVAudioPCMBuffer!, _ : AVAudioTime!) -> Void in
let capacity = UInt32(Float(buffer.frameCapacity)/ratio)
guard let outputBuffer = AVAudioPCMBuffer(
pcmFormat: self.downAudioFormat,
frameCapacity: capacity) else {
print("Failed to create new buffer")
return
}
let inputBlock: AVAudioConverterInputBlock = { inNumPackets, outStatus in
outStatus.pointee = AVAudioConverterInputStatus.haveData
return buffer
}
var error: NSError?
let status: AVAudioConverterOutputStatus = converter.convert(
to: outputBuffer,
error: &error,
withInputFrom: inputBlock)
switch status {
case .error:
if let unwrappedError: NSError = error {
print("Error \(unwrappedError)"))
}
return
default: break
}
self.delegate?.flushAudioBuffer(outputBuffer)
})
}

How to query Healthkit for average heart rate with Swift

I need to query HKStatistics for average heart rate, with Swift 2.2. I've learned from research the parameter I need is HKStatisticsOptionDiscreteAverage
I have code for the workout session. How can I add the function to this code below measuring heart-rate to return the heart rate average of the workout session with HKStatisticsOptionDiscreteAverage?
func createHeartRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {
// adding predicate will not work
// let predicate = HKQuery.predicateForSamplesWithStartDate(workoutStartDate, endDate: nil, options: HKQueryOptions.None)
guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return nil }
let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -> Void in
guard let newAnchor = newAnchor else {return}
self.anchor = newAnchor
self.updateHeartRate(sampleObjects)
}
heartRateQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -> Void in
self.anchor = newAnchor!
self.updateHeartRate(samples)
}
return heartRateQuery
}
func updateHeartRate(samples: [HKSample]?) {
guard let heartRateSamples = samples as? [HKQuantitySample] else {return}
dispatch_async(dispatch_get_main_queue()) {
guard let sample = heartRateSamples.first else{return}
let value = sample.quantity.doubleValueForUnit(self.heartRateUnit)
self.label.setText(String(UInt16(value)))
// retrieve source from sample
let name = sample.sourceRevision.source.name
self.updateDeviceName(name)
self.animateHeart()
}
}
func getAVGHeartRate() {
var typeHeart = HKQuantityType.quantityType(forIdentifier: .heartRate)
var startDate = Date() - 7 * 24 * 60 * 60 // start date is a week
var predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: HKQueryOptions.strictEndDate)
var squery = HKStatisticsQuery(quantityType: typeHeart!, quantitySamplePredicate: predicate, options: .discreteAverage, completionHandler: {(query: HKStatisticsQuery,result: HKStatistics?, error: Error?) -> Void in
DispatchQueue.main.async(execute: {() -> Void in
var quantity: HKQuantity? = result?.averageQuantity()
var beats: Double? = quantity?.doubleValue(for: HKUnit.count().unitDivided(by: HKUnit.minute()))
print("got: \(String(format: "%.f", beats!))")
})
})
healthStore.execute(squery)
}
This is the Swift 3 version :)
this is objective-c example of getting the average heart BPM:
HKQuantityType *typeHeart =[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
HKStatisticsQuery *squery = [[HKStatisticsQuery alloc] initWithQuantityType:typeHeart quantitySamplePredicate:predicate options:HKStatisticsOptionDiscreteAverage completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
HKQuantity *quantity = result.averageQuantity;
double beats = [quantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];
NSLog(#"got: %#", [NSString stringWithFormat:#"%.f",beats]) ;
}
);
}];
[self.healthStore executeQuery:squery];

Definition of Global Variable Resetting

I have a class designed to take the temperature data from an API for a specific date and add it to a dictionary. The URL for the API is stored in a global variable called baseURL. It is defined at the beginning as an empty string, but is later changed. My class is below:
import UIKit
import Foundation
typealias ServiceResponse = (JSON, NSError?) -> Void
class WeatherManager: NSObject {
var baseURL: String = ""
var data: String = ""
static let sharedInstance = WeatherManager()
func getRandomUser(onCompletion: (JSON) -> Void) {
println("Starting getRandomUser")
let route = self.baseURL
println(self.baseURL)
makeHTTPGetRequest(route, onCompletion: { json, err in
onCompletion(json as JSON)
})
}
func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let json:JSON = JSON(data: data)
onCompletion(json, error)
if error != nil {
println("No Error")
} else {
println("Error")
}
})
task.resume()
}
func addData() {
WeatherManager.sharedInstance.getRandomUser { json in
var jsonData = json["response"]["version"]
self.data = "\(jsonData)"
dispatch_async(dispatch_get_main_queue(),{
let alert = UIAlertView()
alert.title = "Weather Data Update"
if self.data != "null" {
println("Value:\(self.data)")
alert.message = "The weather data was updated successfully."
alert.addButtonWithTitle("OK")
alert.show()
} else {
println("Error Reading Data")
println(self.data)
alert.message = "HealthTrendFinder encountered an error while updating data."
alert.addButtonWithTitle("OK")
alert.show()
}
})
}
}
func updateWeatherHistory() {
println(self.baseURL)
let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
println("Weather Updating...")
// This sets the start date to midnight of the current date if no start date has been set.
if StorageManager.getValue(StorageManager.StorageKeys.WeatherStartDate) == nil {
let startDate: NSDate = calendar.startOfDayForDate(NSDate())
StorageManager.setValue(startDate, forKey: StorageManager.StorageKeys.WeatherStartDate)
}
// This adds a data array if it hasn't been created yet.
if StorageManager.getValue(StorageManager.StorageKeys.WeatherData) == nil {
StorageManager.setValue([:], forKey: StorageManager.StorageKeys.WeatherData)
}
var weatherData: [NSDate: NSObject] = StorageManager.getValue(StorageManager.StorageKeys.WeatherData)! as! [NSDate : NSObject]
let startMidnight: NSDate = StorageManager.getValue(StorageManager.StorageKeys.WeatherStartDate) as! NSDate
let currentMidnight: NSDate = calendar.startOfDayForDate(NSDate())
let daysFromStartDate: Int = calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: startMidnight, toDate: currentMidnight, options: nil).day
println("Starting Loop")
for i: Int in 0..<daysFromStartDate {
let dateToBeExamined: NSDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: i, toDate: startMidnight, options: nil)!
if weatherData[dateToBeExamined] == nil {
let calendarUnits: NSCalendarUnit = .CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear
let components = NSCalendar.currentCalendar().components(calendarUnits, fromDate: dateToBeExamined)
var month: String
var day: String
if components.month < 10 {
month = "0\(components.month)"
} else {
month = "\(components.month)"
}
if components.day < 10 {
day = "0\(components.day)"
} else {
day = "\(components.day)"
}
var dateString = "\(components.year)\(month)\(day)"
self.baseURL = "http://api.wunderground.com/api/91e65f0fbb35f122/history_\(dateString)/q/OR/Portland.json"
println(self.baseURL)
var get: () = WeatherManager.sharedInstance.addData()
println(get)
weatherData[dateToBeExamined] = self.data
// There is no data for the NSDate dateForInspection. You need to pull data and add it to the dictionary.
} else {
// Data exists for the specified date, so you don't need to do anything.
}
}
println("Loop has finished or been skipped")
}
}
The problem is, baseURL reverts to an empty string when getRandomUser is executed, after baseURL is set to the URL. Why is this happening, and how do I fix it?
Your code is unnecessarily complex, making it hard to diagnose the problem without more information. But here is a suggestion:
Try making it impossible to instantiate more than one instance of your WeatherManager singleton:
class WeatherManager {
private static let _sharedInstance = WeatherManager()
private init() { super.init() }
static func sharedInstance() -> WeatherManager {
return _sharedInstance
}
}
When you are working from outside WeatherManager, you access it by calling:
let wm = WeatherManager.sharedInstane()
Then, when you are working inside WeatherManager, make sure that all your references are to self - i.e., self.baseURL = ... or self.updateWeatherHistory(), instead of WeatherManager.sharedInstance.baseURL = ..., etc.
Though your code is complicated, I think what is going on is you actually have two instances of WeatherManager in play. You are setting the value of baseURL on one, but not the other. If you want it to be a singleton, you need to make it impossible to create more than one.