startRelativeAltitudeUpdates on a Background OperationQueue

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 swifting.io
// https://swifting.io/blog/2017/10/30/46-locating-a-snowboarder-on-a-slope/
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 ๐Ÿ‚!!!
})

 

topherPedersen