Using @EnvironmentObject properties with CADisplayLink

188 views Asked by At

I'm trying to implement CADisplayLink for some animations, but when I try to access my MainData environment object properties from inside class MyAnimations, I get the fatal error No ObservableObject of type MainData found. A View.environmentObject(_:) for MainData may be missing as an ancestor of this view.

In SceneDelegate, I have MainData set as an environment object on ContentView:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    var mainData = MainData()

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            
            window.rootViewController = UIHostingController(rootView: contentView.environmentObject(self.mainData))
            
            self.window = window
            window.makeKeyAndVisible()
        }
    }

...

}

And here's the class with CADisplayLink. createDisplayLink() is called from ContentView:

class MyAnimations: NSObject{
    @EnvironmentObject var mainData: MainData

    
    func createDisplayLink() {
        let displaylink = CADisplayLink(target: self, selector: #selector(step))
        
        displaylink.add(to: .current, forMode: RunLoop.Mode.default)
    }
    

    @objc func step(link: CADisplayLink) {
        mainData.displayLinkY += 1.5   //Error here
        mainData.displayLinkX += 1.5
    }
    
    
} 

My question is: how can I change environment object properties displayLinkX and displayLinkY from inside step()?

1

There are 1 answers

1
Asperi On BEST ANSWER

Just remove @EnvironmentObject property wrapper, it is for SwiftUI only

class MyAnimations: NSObject{
    var mainData: MainData

    init(mainData: MainData) {
      self.mainData = mainData
        super.init()
    }

    // ... other code
}