0

Scenario:

I have a tabbed-based application with parallel network routines of similar function.

enter image description here

Each has has its own network error handler which sends out a @Published alert flag to notify the calling host's alert operator to display the respective alert.

In this case, I have a 'No Internet' alert.

Problem:

Having only one (1) network error routine is okay... but having multiple-network routines creates multiple network errors which either:

  1. Requires multiple .alert operators (one per tabbed network routine) or
  2. Use a SINGLE .alert operator for all.

Option #1 is bad because all the .alerts would conflict with one-another on the SAME host.
That leaves option #2.

That would require me to have a GLOBAL alert flag that is shared amongst the parallel alert generators.

These alerts originate in parallel 'observable classes', each containing their network-handling logic (publisher) from which the host can harvest their respective alert messages via @Observed vars.

However, each parallel class CANNOT access the @environment to share a global property which would be shared within ONE single .alert() property on the host struct.

That is my problem.

Note: this is merely a learning application to model different network scenarios; and how to process their respective data subjectively. ...I could simplify this by merely checking the network ONCE vs each tabbed routine.

Question: How can I have various classes share a global var (bool flag & String message) generated by a class vs struct?

Would I need a class singleton and how would that be incorporated within the receiving stuct?

I have two worlds: class & strut that can need to share global data.

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • I would use instance of class adopting `ObservableObject` hosting somewhere in `AppDelegate` or `SceneDelegate`, so it could be referenced from other classes and injected via `@EnvironmentObject` in SwiftUI Views. – Asperi Jan 12 '20 at 11:38
  • I had the same idea but I can't access an @EnvironmentObject from a struct. That's my original problem. – Frederick C. Lee Jan 13 '20 at 00:04

0 Answers0