title: Swift: How to put worker into separate thread, show results in main view?
body: <p>I wonder if there is an easy way to do this: Put a long-running worker into a separate thread so as not to block the UI. And display the results in the main view. Under SwiftUI or UIKit. What I found on the web was all very complex. Or is there a completely different approach in Swift?</p>
  <p>I made a minimalistic Python program to show what I want to do. It displays the WiFi signal strength in MacOS.</p>
  <pre><code>import time
  import sys
  import subprocess
  from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout
  from PyQt5.QtCore import QObject, pyqtSignal, QThread
  
  class Worker(QObject):
      send_output = pyqtSignal(str)
      def __init__(self):
          super().__init__()
  
      def worker(self):
          while True:
              _out = subprocess.check_output(
                  [&quot;/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport&quot;,
                   &quot;-I&quot;])\
                  .decode(&quot;utf-8&quot;)
              self.send_output.emit(&quot;RSSI: &quot; + _out.splitlines()[0][-3:] + &quot; dBm&quot;)
              time.sleep(2)
  
  class MainWindow(QWidget):
      do_work = pyqtSignal(object)
      def __init__(self):
          super().__init__()
  
          self.label = QLabel()
          layout = QHBoxLayout()
          self.setLayout(layout)
          layout.addWidget(self.label)
          self.show()
  
          self.app_thread = QThread()
          self.app = Worker()
          self.app.moveToThread(self.app_thread)
          self.app.send_output.connect(self.output_label)
          self.app_thread.started.connect(self.app.worker)
          self.app_thread.start()
  
      def output_label(self, _str):
          self.label.setText(_str)
  
  if __name__ == '__main__':
      application = QApplication(sys.argv)
      mainwindow = MainWindow()
      sys.exit(application.exec())
  </code></pre>
  <p>I'm trying to find my way into Swift right now. It's exciting, but really a big thing. Thanks in advance!</p>