Using a Publisher with orientationChanged

I'm using a published Timer: let currentTimePublisher = Timer.TimerPublisher(interval: 1.0, runLoop: .main, mode: .default) which works fine. However I wish to continually monitor the angle of the iPhone (for a flying app). I can easily code it the old way but I can't figure out how to do it via the Publisher mechanism despite scouring the internet for some days.

orientedChanged and the angle of your device is related, but not quite what I expected when I read the title. orientationChanged seemed to want to reflect if you got the various callbacks that alerted your app that someone dramatically tilted the device. But I think based on what you described, you're more interested in the data from CoreMotion - the raw accelerometer data. There's some documentation on it at Apple Developer Documentation

It's one of the many that are structured as "create an object and use a delegate callback to receive data", which is pretty straightforward to wrap into a publisher with Combine. There's been some recent articles added in Combine, including Using Combine for Your App’s Asynchronous Code, which gives you some raw process. If you want some more detailed patterns/examples to see how it's been done with other delegate based mechanisms, check out the sample code at swiftui-notes/LocationHeadingProxy.swift at master · heckj/swiftui-notes · GitHub, which is referenced in Using Combine - Repeating Publisher from Delegate.

Will do Joseph, thanks for your comments. I will do the research that you suggested.

Best regards


Joseph_Heck

April 18
orientedChanged and the angle of your device is related, but not quite what I expected when I read the title. orientationChanged seemed to want to reflect if you got the various callbacks that alerted your app that
someone dramatically tilted the device. But I think based on what you described, you're more interested in the data from CoreMotion - the raw accelerometer data. There's some documentation on it at

https://developer.apple.com/documentation/coremotion/getting_raw_accelerometer_events

It's one of the many that are structured as "create an object and use a delegate callback to receive data", which is pretty straightforward to wrap into a publisher with Combine. There's been some recent articles
added in Combine, including
Using Combine for Your App’s Asynchronous Code
, which gives you some raw process. If you want some more detailed patterns/examples to see how it's been done with other delegate based mechanisms, check out the sample code at

https://github.com/heckj/swiftui-notes/blob/master/UIKit-Combine/LocationHeadingProxy.swift
, which is referenced in

Using Combine - Repeating Publisher from Delegate
.