NSInputStream stops running, sometimes throws EXC_BAD_ACCESS

后端 未结 2 695
生来不讨喜
生来不讨喜 2020-12-23 10:39

(UPDATED) this is the problem in a nutshell: in iOS I want to read a large file, do some processing on it (in this particular case encode as Base64 string()

2条回答
  •  暖寄归人
    2020-12-23 11:26

    NSStream requires a run loop. GCD doesn't provide one. But you don't need GCD here. NSStream is already asynchronous. Just use it on the main thread; that's what it's designed for.

    You're also doing several UI interactions while on a background thread. You can't do that. All UI interactions have to occur on the main thread (which is easy if you remove the GCD code).

    Where GCD can be useful is if reading and processing the data is time consuming, you can hand that operation to GCD during NSStreamEventHasBytesAvailable.

提交回复
热议问题