-1

I have some code (bluetooth manager) that I want to implement as a singleton for the application.

I read many tutorials online about passing environment variable & using observed object to share states, which is useful to pass data from scene delegate to views, like a list of found BT devices. However, how can views talk back to scene delegate?

For example, when the user click I a device, I would like to connect to it which requires the button click calling some method back into scene delegate where the singleton bluetooth manager resides?

Fangming
  • 24,551
  • 6
  • 100
  • 90
  • 1
    You can see an example [in this SO post](https://stackoverflow.com/a/60134243/12299030). The goal there is different, but the approach is the same. – Asperi Feb 15 '20 at 05:28
  • @Asperi Ouch, right that notification thing. I'm still looking at good way to do it in swiftUI but that should work. Thanks for the info! – Fangming Feb 15 '20 at 05:30

1 Answers1

1

Potential solution:

Create a class conforming to ObservableObject with state variables and methods. Initiate it in scene delegate as a singleton object and then pass it to views as environment object.

Within each view, we will have access to methods & state within that shared singleton.

Fangming
  • 24,551
  • 6
  • 100
  • 90