HealthKit for iOS8: Part 5

iOS 8 HealthKit Santiapps Marcio Valenzuela
iOS 8 HealthKit

Jumping right in, our Energy view controller class starts out like this:

import Foundation

import UIKit

import HealthKit

class EnergyViewController: UITableViewController {

var energyFormatter: NSEnergyFormatter {

var energyFormatter: NSEnergyFormatter?

var onceToken: dispatch_once_t = 0

dispatch_once(&onceToken, {

energyFormatter = NSEnergyFormatter()

energyFormatter?.unitStyle = NSFormattingUnitStyle.Long

energyFormatter?.forFoodEnergyUse = true

energyFormatter?.numberFormatter.maximumFractionDigits = 2

})

return energyFormatter!

}

// No required initWithCoder pasted in...

var  healthStore:HKHealthStore?

@IBOutlet weak var activeEnergyBurnedValueLabel: UILabel?

@IBOutlet weak var restingEnergyBurnedValueLabel: UILabel?

@IBOutlet weak var consumedEnergyValueLabel: UILabel?

@IBOutlet weak var netEnergyValueLabel: UILabel?

var activeEnergyBurned: Double = 0

var restingEnergyBurned: Double = 0

var consumedEnergy: Double = 0

var netEnergy: Double = 0

}

We take care of our imports, subclass UITableViewController, create our NSEnergyFormatter again, our health store property and then we create 4 UILabel outlets to display the values and 4 Doubles to store our energy values to be displayed.  What we are going to do here is just as important as how.  We will have 3 values which will compute a 4th one.  The 4th value is Net Energy which could be positive if we eat more or negative if we exercise more.  The other 3 are:

  • Active Energy Burned which will be populated from any workout sessions.
  • Resting Energy Burned which will be calculated automatically for us based on our profile.
  • Consumed Energy which will be input by the user in the Journal via the FoodPicker.

Ok let’s cover the view controller lifecycle methods.  viewDidLoad will be empty because instead we will use viewWillAppear:

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

self.refreshControl?.addTarget(self, action:"refreshStatistics", forControlEvents:UIControlEvents.ValueChanged)

self.refreshStatistics()

NSNotificationCenter.defaultCenter().addObserver(self, selector:"refreshStatistics", name:UIApplicationDidBecomeActiveNotification, object:nil)

}

override func viewDidDisappear(animated: Bool) {

NSNotificationCenter.defaultCenter().removeObserver(self, name:UIApplicationDidBecomeActiveNotification, object:nil)

}

Here we set our refreshControl to call the refreshStatistics method.  Then we also add ourselves as observer to notifications when the application becomes active.  As always, we clean up on exit when the view disappears.

Now the method that gets called on every viewWillAppear and you’ll understand why it couldn’t be placed in the viewDidLoad:

func refreshStatistics () -> () {

self.refreshControl?.beginRefreshing()

var energyConsumedType: HKQuantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)

var activeEnergyBurnType: HKQuantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)

self.fetchSumOfSamplesTodayForType(energyConsumedType, unit:HKUnit.jouleUnit(), withCompletion: { (totalJoulesConsumed:Double, error:NSError?) -> () in

 

self.fetchSumOfSamplesTodayForType(activeEnergyBurnType, unit: HKUnit.jouleUnit(), withCompletion: { (activeEnergyBurned, error:NSError?) -> () in

self.fetchTotalBasalBurn({ (basalEnergyBurn, error) -> () in

if (basalEnergyBurn == nil) {

NSLog("An error occurred trying to compute the basal energy burn. In your app, handle this gracefully. Error: \(error)")

}

if (basalEnergyBurn != nil) {

// Update the UI with all of the fetched values.

dispatch_async(dispatch_get_main_queue(), {

self.activeEnergyBurned = activeEnergyBurned

self.activeEnergyBurnedValueLabel!.text = self.energyFormatter.stringFromJoules(self.activeEnergyBurned)

self.restingEnergyBurned = basalEnergyBurn!.doubleValueForUnit(HKUnit.jouleUnit())

self.restingEnergyBurnedValueLabel!.text = self.energyFormatter.stringFromJoules(self.restingEnergyBurned)

self.consumedEnergy = totalJoulesConsumed

self.consumedEnergyValueLabel!.text = self.energyFormatter.stringFromJoules(self.consumedEnergy)

self.netEnergy = self.consumedEnergy - self.activeEnergyBurned - self.restingEnergyBurned

self.netEnergyValueLabel!.text = self.energyFormatter.stringFromJoules(self.netEnergy)

self.refreshControl?.endRefreshing()

}) //END OF DISPATCH

}

}) //END OF self.fetchTotalBasalBurn

}) //END OF self.fetchSumOf2..

}) //END OF self.fetchSumOf1...

}

First we set our control to begin refreshing.  Next we create 2 type identifiers for DietaryEnergyConsumed and ActiveEnergyBurned.  Next we call 2 fetches, first for energyConsumedType and then for activeEnergyBurnType.  Then we throw in a basal burn calculation.  We will look at the basal or resting energy burn methods next but, just notice that if they return nil, we log an error, if its not nil, we set our labels to the values of the calculated energies.  Finally we end refreshing.

Ok so let’s look at the meaty methods.  First, the fetchSumOfSamplesTodayForType method:

func fetchSumOfSamplesTodayForType(quantityType:HKQuantityType, unit:HKUnit, withCompletion completion:((Double, NSError?) -> Void)? ) {

let predicate: NSPredicate = self.predicateForSamplesToday()

let query = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: .CumulativeSum) {query, result, error in

let sum = result?.sumQuantity()

if completion != nil {

let value: Double = sum?.doubleValueForUnit(unit) ?? 0.0    //NOTE: use 0.0 when sum is nil

completion!(value, error)

}

}

self.healthStore?.executeQuery(query)

}

Here we create a HKStatisticsQuery fetch which is a bit different from the regular HKQuery.  The reason is that we want to return the .CumulativeSum of all samples, not just the latest one.  We could fetch all samples for today but we would have to add them manually.  Since cumulative sums is quite common, HealthKit has a method for that.  The query returns a result and we set it to sum.  We then return that value in the completion handler.

Now the basal burn or resting energy burn is composed of 3 methods and an extension:

func fetchTotalBasalBurn( completion:(basalEnergyBurn: HKQuantity?, error: NSError?) -> ()?) {

var todayPredicate: NSPredicate = self.predicateForSamplesToday()

var weightType: HKQuantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)

var heightType: HKQuantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)

self.healthStore?.aapl_mostRecentQuantitySampleOfType(weightType, predicate:nil, completion: { (weight, error) -> () in

if weight == nil {

completion(basalEnergyBurn: nil, error:error)

return

}

self.healthStore?.aapl_mostRecentQuantitySampleOfType(heightType, predicate: todayPredicate, completion: { (height, error) -> () in

if height == nil {

completion(basalEnergyBurn: nil, error:error)

return

}

var innerError: NSError?    //NOTE: this var receives errors from dateOfBirthWithError or biologicalSexWithError

var dateOfBirth: NSDate? = self.healthStore?.dateOfBirthWithError(&innerError)

if dateOfBirth == nil {

completion(basalEnergyBurn: nil, error:innerError)

return

}

var biologicalSexObject: HKBiologicalSexObject? = self.healthStore?.biologicalSexWithError(&innerError)

if biologicalSexObject == nil {

completion(basalEnergyBurn: nil, error:error)

return

}

var basalEnergyBurn: HKQuantity = self.calculateBasalBurnTodayFromWeight(weight!, height:height!, dateOfBirth:dateOfBirth, biologicalSex:biologicalSexObject)!

completion(basalEnergyBurn: basalEnergyBurn, error: nil)

})

})

}

It may look daunting…oh who are we kidding, it is.  And it only gets worse!

First we create type identifiers for BodyMass and height.  We also create a predicate calling a separate method which we will see later.  Next we fetch the most recent quantity from the health store.  You will recall we did the exact same thing in the Profile view controller.  We are doing the same thing here but using a helper method thats included in an extension that others can use, not just this method.  So first we ask the health store for the weightType, if nil we return nil in the completion handler.  Next we ask for the weight and ditto with the completion handler.  Next we ask for dateOfBirth and biologicalSex.  This is the same we way asked for DOB in the Profile view controller.  But we have to return a completion handler on both calls, so we do.  Finally we set our basal burn HKQuantity to be saved to the health store by calling the calculateBasalBurnTodayFromWeight and we pass in our values for height, weight, sex and dob.  Once again we return a fully qualified completion handler this time.

So let’s take a look at the calculateBasalBurnTodayFromWeight method:

func calculateBasalBurnTodayFromWeight(weight:HKQuantity?, height:HKQuantity?, dateOfBirth:NSDate?, biologicalSex:HKBiologicalSexObject?) -> (HKQuantity?) {

// Only calculate Basal Metabolic Rate (BMR) if we have enough information about the user

if weight == nil || height == nil || dateOfBirth == nil || biologicalSex == nil {

return nil

}

var lenghtUnit: HKUnit = HKUnit(fromString: "cm")

var someHeight = height?.doubleValueForUnit(lenghtUnit)

let heightInCentimeters: Double = someHeight!

let weightInKilograms: Double = weight!.doubleValueForUnit(HKUnit.gramUnitWithMetricPrefix(HKMetricPrefix.Kilo))

let now: NSDate = NSDate()

let ageComponents: NSDateComponents = NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate:dateOfBirth!, toDate:now, options:nil)

let ageInYears:Int = ageComponents.year

// BMR is calculated in kilocalories per day.

let BMR: Double = self.calculateBMRFromWeight(weightInKilograms, heightInCentimeters:heightInCentimeters, ageInYears:ageInYears, biologicalSex:(biologicalSex?.biologicalSex)!)

// Figure out how much of today has completed so we know how many kilocalories the user has burned.

let startOfToday:NSDate = NSCalendar.currentCalendar().startOfDayForDate(now)

let endOfToday:NSDate = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value:1, toDate:startOfToday, options:nil)!

let secondsInDay: NSTimeInterval = endOfToday.timeIntervalSinceDate(startOfToday)

let percentOfDayComplete: Double = now.timeIntervalSinceDate(startOfToday) / secondsInDay

let kilocaloriesBurned: Double = BMR * percentOfDayComplete

return HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: kilocaloriesBurned)

}

First we make sure we have all the parameters needed to calculate basal or resting energy.  Remember, this is the energy burned by your body, just by carrying out its normal functions, breathing, metabolism etc.  No exercise is included here.

Then we create a unit for the height and format our height.  Ditto for our weight!  Next we calculate our age once again.  Finally we create a BMR variable which will be set by calling yet another method.   The BMR is Basal Metabolic Rate and it is calculated as a percentage of the day that has expired so far.  So basically you burn calories no matter what you do, unless you are dead.  In the morning your body will have burned a certain % of those calories.  As the day progresses, we burn more and so on and so forth.

This means we need to calculate how many seconds (NSTimeInterval) have passed since the beginning of the day.  This gives us a % of the day completed.  We then simply multiply that ratio by the BMR that a person with that weight, height, sex and age is supposed to have in a day and we get our result.

Finally we return an HKQuantity for the kilocalories burned according to the BMR and the % of day complete.

Here is a small method to take a break:

func predicateForSamplesToday () -> (NSPredicate) {

let calendar: NSCalendar = NSCalendar.currentCalendar()

let now: NSDate = NSDate()

let startDate: NSDate = calendar.startOfDayForDate(now)

let endDate: NSDate = calendar.dateByAddingUnit(.CalendarUnitDay, value:1, toDate:startDate, options:nil)!

return HKQuery.predicateForSamplesWithStartDate(startDate, endDate:endDate, options:HKQueryOptions.StrictStartDate)

}

What we are doing here is creating a start and end date for today instead of having to re-write that code everytime.

So how do you calculate BMR:

func calculateBMRFromWeight(weightInKilograms:Double, heightInCentimeters:Double, ageInYears:Int, biologicalSex:HKBiologicalSex) -> (Double) {

var BMR:Double

if (biologicalSex == HKBiologicalSex.Male) {

BMR = 66.0 + (13.8*weightInKilograms) + (5*heightInCentimeters) - (6.8*Double(ageInyears))

} else {

BMR = 655.0 + (9.6*weightInKilograms) + (1.8*heightInCentimeters) - (4.7*Double(ageInyears))

}

return BMR

}

It’s sex dependent and basically a simple linear formula.  Big whoop.  Ok.

Now let’s look at the meaty helper extension we saw earlier:

extension HKHealthStore {

func aapl_mostRecentQuantitySampleOfType(quantityType: HKQuantityType, predicate:NSPredicate?, completion:( (HKQuantity?, NSError?) -> () )? )  {

var timeSortDescriptor: NSSortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierEndDate, ascending:false)

var query =   HKSampleQuery(sampleType: quantityType, predicate: nil, limit: 1, sortDescriptors: [timeSortDescriptor]) { query, results, error in

if (results == nil) {

if (completion != nil) {

completion?(nil, error)

} //END OF IF 2...

return

} // END OF IF 1...

if ((completion) != nil) {

// If quantity isn't in the database, return nil in the completion block.

var resultados = results as NSArray

var quantitySample: HKQuantitySample = resultados.firstObject as HKQuantitySample

var quantity: HKQuantity = quantitySample.quantity

completion?(quantity, error)

} //END OF IF

} // HKSAMPLEQUERY

self.executeQuery(query)

} // END OF FUNCTION

} //END OF EXTENSION

We create a sort descriptor as we did in the Profile, because we want to order our results by the latest.  Then we create our simple HKQuery with whatever quantityType was passed in, the predicate, limit and sortDescriptor as well as the completion handler.  If results are nil, we set our completion handler value to nil and return.  Otherwise, we set our completion handler to whatever the most recent quantity fetched is because once again we take the firstObject from that results array.

Ok the only thing left here is to prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

var workoutViewController: WorkoutViewController = segue.destinationViewController as WorkoutViewController//viewController as WorkoutViewController

workoutViewController.healthStore = self.healthStore!

}

The reason we segue is that we need to take the user to the point where he can enter the only value missing.  We already calculated the resting energy value internally based on the user’s profile data.  Then we calculated the consumed energy based on what the user picked as his or her consumed foods.  So now we need to know how much energy he burnt in his workout.

Race you there (Part 6)!

 

HealthKit for iOS8: Part 3

HealthKit iOS8 by Santiapps.com
HealthKit iOS8

Great!  Now let’s move on to the Journal View Controller.  It’ll be a good relax! 🙂 Ok we start out importing the frameworks we need:

import UIKit
import HealthKit

Ok now let’s declare our class properties:

class JournalViewController: UITableViewController {     
let JournalViewControllerTableViewCellReuseIdentifier: NSString = "Cell"     
var foodItems: NSMutableArray?     
var energyFormatter: NSEnergyFormatter {         
var energyFormatter: NSEnergyFormatter?         
var onceToken: dispatch_once_t = 0         
dispatch_once(&onceToken, {             
energyFormatter = NSEnergyFormatter()             
energyFormatter?.unitStyle = NSFormattingUnitStyle.Long             energyFormatter?.forFoodEnergyUse = true             
energyFormatter?.numberFormatter.maximumFractionDigits = 2         })         
return energyFormatter!     
}      
var  healthStore:HKHealthStore? 
}

The first thing to note, is that once again this is UITableViewController.  However, unlike Profile, this tableview will contain dynamic, prototype cells.  Thus we need to dequeue our cells to save memory.  The first constant we need is one for our table view cell identifier.  Then we create a mutable array to hold our selected food items, which will come from the user selecting them in the FoodPicker. The next property is a bit strange.  NSEnergyFormatter is something new and health kit uses it to format energy data.  This is a computed property, which means it is computed each time you call for it.  This is basically creating an energyFormatter much in the same way you would create an dateFormatter from NSDateFormatter. Then finally there is the healthstore property which we need and set from the AppDelegate as you recall. Next up we have our view controller lifecycle methods which kick things off:

override func viewDidLoad() {         
super.viewDidLoad()         
self.foodItems = NSMutableArray()         
self.updateJournal()         
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateJournal", name: UIApplicationDidBecomeActiveNotification, object: nil)     }     
override func viewDidDisappear(animated: Bool) {         
NSNotificationCenter.defaultCenter().removeObserver(self, name:UIApplicationDidBecomeActiveNotification, object:nil)     
}

Our viewDidLoad does 2 things; instantiates a mutable array for foodItems so the user can start putting things in there and it calls updateJournal which we will write next.  This view controller also sets itself up as an observer for notifications fired whenever the app becomes active and finally in the viewDidDisappear method we remove ourselves as an observer. Now let’s write updateJournal.  The purpose of this method will be to basically update this tableview with data gotten from the FoodPicker after the user selects items he or she consumed:

func updateJournal () -> () {
var now: NSDate = NSDate()
let calendar : NSCalendar = NSCalendar.currentCalendar()       
var components: NSDateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: now)
var startDate: NSDate = calendar.dateFromComponents(components)!
var endDate: NSDate = calendar.dateByAddingUnit(.CalendarUnitDay, value:1, toDate:startDate, options:nil)!

var sampleType: HKSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)
var predicate: NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate:endDate, options:.None)

var query: HKSampleQuery = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: 0, sortDescriptors: nil) {

(query:HKSampleQuery?, results:[AnyObject]!, error:NSError!) -> Void in

if (error != nil) {
NSLog("An error occured fetching the user's tracked food. In your app, try to handle this gracefully. The error was: %@.", error)                 
abort()
}
if results != nil {
NSLog("Got something!")
}
dispatch_async(dispatch_get_main_queue(), {

// PARSE RESULTS INTO ARRAY, RELOAD DATA ALL ON MAIN QUEUE
self.foodItems?.removeAllObjects()
for sample in results as [HKQuantitySample] {                 
NSLog("sample is= \(sample.metadata)")
let foodMetadataDict = (sample.metadata as NSDictionary)
let foodThing: AnyObject? = foodMetadataDict.objectForKey("HKMetadataKeyFoodType")                 NSLog("\(foodThing)")

let foodName = foodThing as NSString
let joules: Double = sample.quantity.doubleValueForUnit(HKUnit.jouleUnit())             
let foodItem: FoodItem = FoodItem(name: foodName, joules:joules) as FoodItem                 self.foodItems?.addObject(foodItem)
}             
self.tableView.reloadData()
})
}
self.healthStore?.executeQuery(query)     // EXECUTE QUERY  }

Ok, its a bit long but simple.  The first few lines create a date for our query.  Then we create a sampleType for DietaryEnergyConsumed which is a data type health store can read and write.  We then create a predicate for it which basically limits the query to today.  Finally we create the query object and execute it (in the last line).

Remember, whats inside the query body is actually a completion block.  We pass the query the sampleType, dates and completion handler.  That completion handler takes a result and an error once again.  If there is an error, we log, if we get results from the query then we log that we got something!  Thats just being silly, but then the query code goes on to a dispatch queue.

This is where we will now update the UI by removing all objects in our foodItems array, and then for every HKQuantitySample in results array, we get the metadata key that we  will store in the object array as part of the FoodItem object and get it as a NSString.

Finally we get the value for that quantity and create a fully qualified FoodItem with that data.  In the end we add that FoodItem to the array and reload the table data. Well now that we mentioned FoodItem, let’s take a look at that class and it’ll help you better understand the next method which is the actual method for adding a FoodItem’s data to the health store.  So here is the FoodItem class:

import Foundation
import HealthKit

class FoodItem {
var name: NSString
var joules:  Double
init (name:NSString, joules:Double) {
self.name = name
self.joules = joules
}

func isEqual(object:AnyObject) -> (Bool) {
if object.isKindOfClass(FoodItem) {
return ((object as FoodItem).joules == self.joules) && (self.name == (object as FoodItem).name)
}
return false;
}

func description(NSString) -> (NSString){
var descriptionDict: NSDictionary = [ "name" : self.name, "joules" : self.joules ]
return descriptionDict.description as NSString
}
}

That was short and sweet!  First we declare the properties of our FoodItem class, name and joules.  Then we create the initializer for it, important to note!  We also added an isEqual function in there to test if an object is a FoodItem type object.  Remember that FoodItem is not subclassing NSObject so it doesn’t have access to NSObject’s isEqual function.  Finally we give FoodItem a description function which basically returns its name and joules values as an NSString. Sweet!  Ok so now let’s look at the Journal view controller method that actually adds a FoodItem:

func addFoodItem (foodItem:FoodItem) {         
// MUST DEFINE HKQUANTITY_TYPE         
var quantityType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)         //MUST DEFINE HKQUANTITY         
var quantity: HKQuantity = HKQuantity(unit: HKUnit.jouleUnit(), doubleValue:foodItem.joules)         //DATE & METADATA         
var now: NSDate = NSDate()         
var metadata: NSDictionary = ["HKMetadataKeyFoodType":foodItem.name]         
//This creates the object to SAVE         
var calorieSample: HKQuantitySample = HKQuantitySample(type: quantityType, quantity:quantity, startDate:now, endDate:now, metadata:metadata)          
NSLog("Before saving to health store in Journal...")         self.healthStore?.saveObject(calorieSample, withCompletion: { (success, error) in             NSLog("After saving to healthstore in Journal...")             dispatch_async(dispatch_get_main_queue(), {                 
NSLog("After dispatch to health store in Journal...")                 
if success {                     //MUST UPDATE TABLE in MAIN QUEUE                     NSLog("Energy Consumed Saved")                     
self.foodItems?.insertObject(foodItem, atIndex:0)                     
var indexPathForInsertedFoodItem: NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)                     self.tableView.insertRowsAtIndexPaths([indexPathForInsertedFoodItem], withRowAnimation: UITableViewRowAnimation.Automatic)                 
} else {                     
NSLog("An error occured saving the food %@. In your app, try to handle this gracefully. The error was: %@.", foodItem.name, error)                     
abort()                 
}             
})         
})     
}

It seems long, but the pattern is pretty simple.  We create the right identifier for DietaryEnergyConsumed, we create a quantity from the FoodItem we passed into this method, specifically from its joules value.  We create a date and give it some metadata in order to complete the creation of our HKQuantitySample.  Finally we saveObject to the health store and in its completion block, if success we log Energy Consumed Saved, insert the newly selected item into the foodItems array and update the tableview.  Otherwise we log the error. Finally let’s look at our tableview methods and our segue method because remember that from this Journal view controller we will allow the user to tap the “Add” button to take us to the FoodPicker which we will analyze next.  So here are our tableview and segue methods:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return self.foodItems!.count     }

We set our numberOfRowsInSection to the items in our mutable foodItems array.

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {         
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath!) as UITableViewCell          
var foodItem: FoodItem = self.foodItems?[indexPath!.row] as FoodItem         cell.textLabel.text = foodItem.name          
cell.detailTextLabel!.text = energyFormatter.stringFromJoules(foodItem.joules)         
return cell;     
}

Here we dequeue a cell as always and create a FoodItem according to the indexPath!.row and get its name and joules into the cell.

func performUnwindSegue(segue:UIStoryboardSegue) { //was typed as ibaction...         
var foodPickerViewController: FoodPickerViewController = segue.sourceViewController as FoodPickerViewController         
var selectedFoodItem: FoodItem = foodPickerViewController.selectedFoodItem! as FoodItem       self.addFoodItem(selectedFoodItem)     
}

As for the segue, this is the initiating view controller, so we need an unwind because when the user finishes selecting an item in the FoodPicker, we must do some stuff.  What stuff?  Well first we get our sourceViewController for the “unwind” segue.  Don’t get confused because normally you get the destination segue of a forward segue in order to set that destination view controller’s properties.  In this case, we are in the calling view controller and we are unwinding from a segue.  Our source view controller is FoodPicker and our destination is Journal.  We then take the source’s selectedFoodItem property and set it as a local variable called selectedFoodItem here, locally, in Journal.  Then we call our addFoodItem() method passing it in that local variable selectedFoodItem.  That saves that food item’s name in metadata and its joules into the health store as DietaryConsumedEnergy. Well, that was quite simple.  The FoodPicker is actually quite simple.  See you there (Part 4)!

Swift Closures Quick Reference: Part 3

Now let’s write our own closure!

func fetchMostRecentDataOfQuantityType(quantityType: HKQuantityType, withCompletion completion: ((mostRecentQuantity:HKQuantity?, error:NSError?) -> ())? ) {

let timeSortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)

[timeSortDescriptor], resultsHandler: { (query:HKSampleQuery!, results:[AnyObject]!, error:NSError?) -> Void in

let query = HKSampleQuery(sampleType: quantityType, predicate: nil, limit: 1, sortDescriptors: [timeSortDescriptor]) { query, results, error in

if completion != nil && error != nil {

completion!(mostRecentQuantity: nil, error: error)

return;

}

let resultsArray = results as NSArray?

var quantitySample: HKQuantitySample? = resultsArray?.firstObject as HKQuantitySample?

var quantity: HKQuantity? = quantitySample?.quantity

if completion != nil {

completion!(mostRecentQuantity: quantity, error: error)

}

}

self.healthStore?.executeQuery(query)

}

Notice we declare a function that takes a quantityType parameter and a completion parameter.  The completion parameter is a closure (it also has an internal and external name).  The closure is defined as:

{ (mostRecentQuantity, error) -> () } // which takes 2 parameters itself and returns void *dont worry about the ?

Internally, the function uses the completion block.  Whenever we wish to call the completion block, we test for it and say completion (mostRecentQuantity, error) which sets the completion block.  Again don’t worry about the ?

Now let’s actually use that function in code.  We call it with self and pass it in heightType as the quantityType parameter and pass it the values for mostRecentQuantity and any error and the code to be executed.  That code takes the error and checks if there is any in order to log it.  Otherwise, it takes mostRecentQuantity and uses it to set a local variable.

self.fetchMostRecentDataOfQuantityType(heightType, withCompletion: { (mostRecentQuantity:HKQuantity?, error:NSError?) -> () in

if let something = error {

NSLog(“An error occured fetching the user’s height information. In your app, try to handle this gracefully. The error was: %@.”, something)

abort()

}

//Determine the height in the required unit.

var usersHeight: Double = 0.0

if let somethingElse = mostRecentQuantity {

var heightUnit: HKUnit = HKUnit.inchUnit()

usersHeight = mostRecentQuantity!.doubleValueForUnit(heightUnit)

// Update the user interface.

dispatch_async(dispatch_get_main_queue(), {

self.heightValueTextField.text = NSNumberFormatter.localizedStringFromNumber(usersHeight, numberStyle:.NoStyle)

})

}

})

Here is another example of one we can write.  I actually borrowed this from a web tutorial and a GitHub post:

func searchUsersWithSearchTerm(searchTerm: String, completionClosure: (users :User[]) ->()) {
      var request = NSMutableURLRequest(URL: NSURL(string: "https://api.github.com/search/users?q=\(searchTerm)"))
      request.HTTPMethod = "GET"
      let postDataTask = self.urlSession.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
           if error {
                println(error.localizedDescription)
           }
           println(response)
           var repoJSON : NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: nil) as NSMutableDictionary
           var jsonArray = repoJSON["items"] as NSMutableArray
           var users : User[] = User().parseJSONIntoUsers(jsonArray)
           println(users.count)
           completionClosure(users: users)
      })
      postDataTask.resume()
 }

The function takes a searchTerm parameter and a completionClosure much like the previous one.  That closure is:

{ (user:[User]) -> ( )  } // takes a User array named user and returns void.

The function performs some heavy-slow work and then ends up populating an array of Users.  Once we have our data, we want to call our completion block.  So normally you would use a parameter inside a function to do something.  Here we are using our closure parameter to do something, call it!  We call it by saying closure(parameters) as if we were calling a function().  To call a function you pass in the parameters that function requires.  To call a closure you call the parameters that closure requires.  In this case, our closure only requires one parameter, the users array.  So basically the code inside the function will be executed serially and only after the data is returned from the fetch, will the completion closure be called.  Once its called, then it will do whatever was passed into it inside { }.

Now how would we call this function:

self.someController.searchUsers("Woz") { (users: User[]) in
    self.searchUsers = users
    NSOperationQueue.mainQueue().addOperationWithBlock() { () in
        self.collectionView.reloadData()
    }
}

We call the function by passing it the two parameters it requires (searchTerm and completion closure).  Normally we would say (param1,param2), but when the last parameter of a function is a closure, we are allowed to tag it as:

function(param1) { //closure code }

In short, when you call a function that has a completion closure as a parameter you pass it the parameters it needs to execute it’s own code and the closure to execute afterwards.  It first executes it’s own code (which in our case got data from the web) and then that same code is in charge of calling a completion closure only after it’s done with it’s own code.  By calling completion(), the function is calling the completion closure which contains the code we passed it.

How to properly use pre-made closures:

1. var = method(param1, param2, param3) handler4{ params in //if code } //handler4 is optional
        OR
2. var = method(param1, param2, param3, handler:{() -> Void in //if code })

Hope you enjoyed this.