What is the difference between NSTimer
, NSTask
, NSThread
and NSRunloop
and is there a guideline on when to use each of them?<
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.