title: How to clean up environment object after Swiftui view is dismissed?
body: <p>I have a fairly large project that I started with storyboards and xibs. Now I'm implementing some new views using swiftui. This is the code I'm using to do that:</p>
  
  <pre><code>func goToSchedule() {
      let scheduleController = ScheduleController()
      let vc = UIHostingController(rootView: ScheduleView().environmentObject(scheduleController))
      if let topController = UIApplication.topViewController() {
          topController.present(vc, animated: true, completion: nil)
      }
  }
  </code></pre>
  
  <p>But I have a problem when I dismiss (swipping down) the view and open it again, every time I do it creates an extra copy of the environment object, this object is a class that handles a tcp connection, the shortened code is as follows:</p>
  
  <pre><code>class ScheduleController: ObservableObject, NetworkDelegate {
      var didChange = PassthroughSubject&lt;Void, Never&gt;()
      @Published var timers = [ScheduleTimer]()
      let connection = tcpNetwork()
      init() {
          connection.delegate = self
      }
      func send(data: String) {
          self.connection.sendMsg(data)
      }
      func processData(data: String) {
      // long function where the received data is parsed and the timers array is updated
  }}
  </code></pre>
  
  <p>The view is this:</p>
  
  <pre><code>struct ScheduleView: View {
  @EnvironmentObject var scheduleController: ScheduleController
  
  var body: some View {
      NavigationView {
        List {
          ForEach(scheduleController.timers, id: \.self) { timer in
              TimerRow(id: timer.id, name: timer.name, startTimer: timer.start, endTimer: timer.end, isActive: timer.isActive)
          }
        }}
  </code></pre>