Would NSTImer events block the main thread?

前端 未结 2 741
心在旅途
心在旅途 2021-01-06 01:06

When we use a NSTimer, once the call back is called after the mentioned interval, does the UI would be blocked?

2条回答
  •  执念已碎
    2021-01-06 01:37

    That depends. Most of the time, this won't be a problem.

    If, however, both of the following criteria are met, an NSTimer will block the UI thread:

    1. The timer is scheduled on the NSRunLoop of the main thread. This will be the case whenever you created it by calling one of NStimer's scheduledTimerWith... class-methods on the main thread.
    2. The method, which is called when the timer fires, performs "lengthy" tasks. Things like synchronously performed fetches/url-requests come to mind...

提交回复
热议问题