NSTimer requiring me to add it to a runloop

前端 未结 6 1938
盖世英雄少女心
盖世英雄少女心 2020-12-28 16:27

I am wondering if someone can explain why dispatching back to the main queue and creating a repeating NSTimer I am having to add it to RUN LOOP for it too fire?

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 16:51

    Because, as the docs say:

    Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

    It is a design decision that Apple made when they wrote the code for NSTimer (and I'm sure they had good reason to do so) and there is nothing we can do to get around it. Is it really that burdensome?

提交回复
热议问题