title: How to efficiently fill a UITextView with streaming content?
body: <p>I have an iOS app which I use to trigger an action. This action produces a lot of text, and I want to show it to the user as it updates. My current method chokes on longer (5k line) output. What can I do to handle this beyond truncating after a few hundred lines?</p>
  
  <hr>
  
  <p>Currently what I do is have the process stream output to a file. Then I take a <code>FileHandle</code> on this file, and create a <code>readabilityHandler</code> which passes the snippet to my <code>UIViewController</code>:</p>
  
  <pre><code>stdout_handler = FileHandle(forReadingAtPath: "my.txt")
  stdout_handler?.readabilityHandler = { handle in
      let data = handle.readData(ofLength: 100)
      if let snippet = String(data: data, encoding: .utf8) {
          self.delegate?.append_script(text: snippet)
      }
  }
  </code></pre>
  
  <p>I have my <code>UIViewController</code>, the delegate, take this string and append it to the <code>UITextView.text</code></p>
  
  <pre><code>func append_script(text: String) {
      DispatchQueue.main.async {
          if self.script_output.text == nil {
              self.script_output.text = text
          } else {
              self.script_output.text.append(text)
          }
  
          let last_line = NSRange(location: self.script_output.text.count - 1, length: 1)
          self.script_output.scrollRangeToVisible(last_line)
      }
  }
  </code></pre>
  
  <p>In the simple case, it's good enough; but surely there's a better way.</p>