startRelativeAltitudeUpdates on a Background OperationQueue

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today!

Doing a little hacking on my WingsuitGP app tonight so I can take it for a test flight tomorrow. The original prototype used a really ugly work-around to access altitude (pressure) data in the background, so I would like to make a note here of an excellent blog post demonstrating how to properly run altitude updates in the background on iOS: Locating a Snowboarder on a Slope
The example snippets of code I’ve found around the web regarding startRelativeAltitudeUpdates all had the updates running on OperationQueue.main, but after discovering the aforementioned mentioned blog post, it looks like it’s fairly simple to create your own OperationQueue and specify that you would like it to run in the background. I’m sure an experienced iOS developer would have been able to figure this out in 5 min., but… o well! Now I know 🙂

// Code Snippet by
let queue = OperationQueue()
queue.qualityOfService = .background
queue.maxConcurrentOperationCount = OperationQueue.defaultMaxConcurrentOperationCount
let altimeter = CMAltimeter()
altimeter.startRelativeAltitudeUpdates(to: queue,
    withHandler: { data, error in //(CMAltitudeData?, Error?)
        guard error == nil else { return }
        guard let data = data else { return }
        //NOTE: process data 🏂!!!