UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Can HKWorkout be made Codable?

Forums > SwiftUI

@ASH  

I'm thinking about developing a workout tracking app. To avoid the issue of having to repeatedly download workouts, I want to persist some app model data like Apple does with their Fitness App. At a minimum, the HKWorkout class. Has anyone seen this done before? I'd hate to have reinvent the wheel. HKWorkout: HKSample: HKObject: NSObject.

2      

I don't think that someone did this before. There was no reason for doing so because in the beginning health data was only available on your phone. Now you can store it in iCloud and it's available on all your devices (if you choose to use iCloud).

But if you choose to actually track a workout I suppose you use a watch as well. The watch syncs with your phone and the data is available on your phone. So the request to list your data from the phone on the phone is pretty much non existent. The data is just there. As long as you don't have a specific use case I don't think it's worth the effort. But perhaps you're trying something different. So let us know :)

2      

@ASH  

My English wasn't the best. My use case would be something like this:

Open app, load the workout data, scroll workouts, select a workout do something, go back, select another workout, do something else, quit the app.

Another day, repeat the same thing, except, I don't want to have to sit through the loading of data twice. After the first slog is downloaded I just want to load the deltas. Hence, I'm looking to persist some of the data.

The HKWorkout is key if you want to look at high frequency data like HR, pace, power, etc and it's not codable out of the box.

2      

Hm, okay. I think there is a slight misunderstanding. After finishing a workout and saving the HKWorkout to the Health App it's immutable. You can not continue a HKWorkout after that.

With iOS17 the initializers of HKWorkout are even deprecated, you should always create a HKWorkout via HKWorkoutSession. You can read finished workouts (=HKWorkout) from the Health App but you aren't able to add anything to them. This goes for all samples you saved to the Health App. You can just delete but not edit them.

I think what you're trying to accomplish is to have a workout app with your own specific workout types and save them to the Health App. But for this you don't need HKWorkout. HKWorkout is now used by the Apple frameworks and you don't need to touch it. Perhaps some extension to it to get your custom metadata easier.

Please have a look at the documentation Running workout sessions how to start a workout.

When you want to link several workouts over more than one session you would need to store the data in the metadata of the workout event to identify them as a group.

Please bear in mind: You can have only one active workout session (=HKWorkoutSession) at all times. This means if you start a new workout session without ending the first one the first one is ungracefully killed.

I created a custom workout app and you can test it if you want. At no place in my code I create a HKWorkout on my own. I just add workout events to the current running workout (including some metadata for my custom workouts) to get the listings I need. What is more is, you can show the workout in the Apple Fitness app like any other workout done with the Apple workout app. Just without my custom data. This is shown in my own workout app.

And yes, the data model to create my workout plan with exercises and algorithms is completely custom. No HKWorkout was harmed in the process ;) On the other hand, exercising and tracking my workout is done on base of the above linked guide. For my done workout list I query the Health App and get back a lot of HKWorkouts which I then present with my custom metadata.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.