Now we must add CoreData. To do this we need to create a CoreData stack and a xcdatamodeld file. First thing is first, let’s add the xcdatamodeld file by New->File->CoreData->DataModel. Name it SwimModel. Create an entity called Swim and add the following attributes:

pace : Int16

date : Date

laps : Int16

meters : Double

totalTime : Double

Now with the xcdatamodeld file selected, go to Editor and select Create NSManagedObject subclass:

HealthKit for iOS8

Make sure SwimModel is selected, click Next, make sure to select the Swim entity, click Next and you should get a Swim.swift class like this:

Now since this is not a CoreData tutorial, I will not go into the details, but every CoreData project needs a MOM, MOC and PSC. That is what we initialize here.

Now we can begin writing to our MOC and PSC. To test run it, let’s hardcode a value. Go to the SwimmingData Class and first give it an import CoreData at the top. Now declare a property for your stack inside your class of course:

And don’t forget to set your cell type to Right Detail in your storyboard. If you Build & Run and switch to the newly created tab, you might get a crash saying:

Unable to load class Swim …

This is because you need to fully qualify the class name in CoreData, so select your xcdatamodeld file and with your Swim entity selected, make sure to append the Class name in the inspector on the right like so:

HealthKit for iOS8

Basically you need to ensure that you append your project name to the Class name field.

Now run your app and go over to the Workouts tab and see your hardcoded workout in the tableview.

Before we move on, let’s take a few minutes to work on some details. While this provides the info required by the user, it would be nice to polish it up a bit. First, we should add the letters “mins” to the totalTime displayed in the cell. Second, it would be nice to format the date a little more such that its more human readable. So go back to your cellForRowAtIndexPath and make the following changes:

There, now the user has a little mode detailed info of the data displayed. We could go on and modify the cell to hold more data or even be selectable such that it would segue into a detail view controller to display all the info.

WRITING TO CORE DATA

Now all that is left to do is actually, remove that viewDidLoad code that writes to CoreData and instead, write to CoreData from our Workout view controller. So back in WorkoutViewController, first import CoreData at the top, then add this property:

lazy var coreDataStack = CoreDataStack()

and finally, in the saveMyWorkout method, after we calculate our joules burned, or before, it doesn’t matter, add this code:

This is a stored fetch request and to use it you must head over to the xcdatamodeld.file and create a new FetchRequest, leave its name as FetchRequest and now from the editor leave Swim as the selected entity to fetch from. Now go back to SwimmingData and add this method:

Alternatively you can also use NSFetchedResultsController. NSFRC is a neat object that is created specifically for fetching and manipulating data from a CoreData query. Its special in many respects but mainly because it works nicely with table views. It can store information about table structure and can allow for interaction between its data and the tableview at the same time. So add this property to the top of your class: