Swift Package for Fit File Parsing

Garmin sends ConnectStats all the data as Fit Files, so I had built a library to parse it for the app. I used the code from the official Fit SDK. Originally I used the c++ implementation provided in the SDK as it was providing a way to get all the data pretty generically. The SDK while quite generic was actually quite slow in parsing.

About 2 years ago, as I wanted to get ConnectStats to get the extra information from the fit file and parse them systematically for each activity, the speed started to become an issue. So I wrote a new library in swift based on the c implementation of the SDK.

That ended up much faster than the previous version, and ConnectStats has been using it for a while, as well as FitFileExplorer which is a tool I wrote to see the raw fit files.

While I had provided that library on GitHub and it was open source, it wasn’t really easy to integrated into another app. Following a request last week, I investigated how to make it a Swift Package which I wasn’t really aware of. Always a good challenge to learn something new!

I turns out that Swift Packages are extremely simple and useful. Moving the code to a package was fairly straight forward, I only found two technical issues to solve for my use case.

First you need to create a different package for each languages. Easy enough, the code uses c, objective c and swift, so I had to split it into three sub packages.

Second for testing, swift packages don’t seem to provide mechanism to include files in the test applications, and I needed to include a fit file for the test. But I just figured out I could just refer to the current directory where the code is checked out with #file, given that when you run the test, the code will be there. A bit of a hack, but simple and it works.

I hope this will make this library useful for others as it is now easy to integrate parsing of fit file in an iOS or macOS app, given using a Swift Package in an Xcode project is ultra easy!

2 thoughts on “Swift Package for Fit File Parsing

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.