What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?

前端 未结 3 530
我在风中等你
我在风中等你 2021-01-29 19:26

What is the difference between NSTimer, NSTask, NSThread and NSRunloop and is there a guideline on when to use each of them?<

3条回答
  •  不思量自难忘°
    2021-01-29 19:41

    The other answers do a pretty good job of summing up timers, tasks, and threads. I wanted to comment a bit more about the NSRunloop as I think most of the other answers still leave some confusion here. From the NSRunloop documentation:

    An NSRunLoop object processes input for sources such as mouse and keyboard events from the window system, NSPort objects, and NSConnection objects. An NSRunLoop object also processes NSTimer events.

    also

    In general, your application does not need to either create or explicitly manage NSRunLoop objects. Each NSThread object, including the application’s main thread, has an NSRunLoop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method currentRunLoop.

    Think of the NSRunloop as the main event processing and dispatching loop for a particular thread. It reads from the input devices, services any objects that need to be serviced, and dispatches the data appropriately.

提交回复
热议问题