Swift: How to read standard output in a child process without waiting for process to finish

后端 未结 1 456
忘了有多久
忘了有多久 2021-02-04 11:22

I have an external console application (on OS X) that emits a sequence of integers from 1 to 100 to standard output, roughly once every second.

I Swift, I need to use th

1条回答
  •  温柔的废话
    2021-02-04 12:08

    You are reading synchronously on the main thread, therefore the UI is not updated until the function returns to the main loop.

    There are (at least) two possible approaches to solve the problem:

    • Do the reading from the pipe on a background thread (e.g. by dispatching it to a background queue – but don't forget to dispatch the UI updates to the main thread again).
    • Use notifications to read asynchronously from the pipe (see Real time NSTask output to NSTextView with Swift for an example).

    0 讨论(0)
提交回复
热议问题