How do I access an environment object from within a non-View Class?

tl;dr - I've got a data model class and a Multipeer controller class. I would like to access my data model from the (non-view)Multipeer controller class.

I'm working on a tvOS app that will display data in a certain way. The data is in my class model which has all vars @Published. The Class is declared a @StateObject in my base app file and passed to the views as an .environmentObject. This works fine for accessing/changing values in the data model from any view.

I have another Class defined that establishes a Multipeer Connectivity connection to peers and listens for data to be sent. When the data is sent I decode it into my data model Class. This works fine.

The problem is that I need to stuff these received values into the StateObject data structure that all the views use. I have not successfully been able to do that.

Any suggestions on how to get the data I've received in this one class into the "global" data structure that the Views access?

You cannot use environment objects and values outside of the SwiftUI hierarchy (apps, scenes and views).

If you need to refer to these objects from other code, you will need to use some kind of global referencing mechanism (Model.shared, or something like that) or have some kind of lookup mechanism that returns the reference or value.

There's no standard way to do this. You'll need to find an approach that works for your particular data structures.

I simply needed to pass over some data that was received via Multipeer Connectivity so I just stuffed the received values into UserDefaults and was able to retrieve them from my View.

How do I access an environment object from within a non-View Class?
 
 
Q