What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?
What is the difference between
NSRunloop and is there a guideline on when to use each of them?
- Input from the keyboard in command line application
- Simple Swift Cocoa app with WebKit: Upload picture doesn't work
- Remove NSWindow content view
- How to get UITableViewCell indexPath from the Cell?
- How to write data in plist?
- Good practice for disambiguating argument names versus instance variable names in Objective-C
3 Solutions Collect From Internet About “What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?”
Each program runs in at least one thread. You can think of each thread as a separate process of program execution, each running parallel to the others.
If you have some kind of user interface, or other code that needs to listen to events (like network ports), you need a run loop. Every NSThread automatically gets its own run loop, and you very rarely have to concern yourself with them directly. The run loop is also in charge of creating and releasing autorelease pools.
[EDIT: See comments for more discussion about autorelease pools. The most important point to keep in mind is that new threads must take care of setting up an autorelease pool. For example, methods that are invoked with detachNewThreadSelector (see below) should have the following as their first and last lines:
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; [code here] [pool release];
The same applies for threads spawned using other techniques.]
In the main thread, where all the UI handling is taking place, the run loop is very important, since it keeps the interface reactive. That’s why you should never run code that’s time consuming on the main thread: it will eat up all time on the thread and the run loop will not be allowed to run often enough, resulting in a locked or slow interface. If you need to perform time consuming calculations, or keep a task running in the background, you should create a new thread. Again, you probably don’t have to think about the new run loop being created. An easy way of performing a method in a new thread:
[NSThread detachNewThreadSelector:@selector(theSelector) toTarget:self withObject:nil];
Inter-thread communication can be tricky, and you should be aware of the methods
(Great tips on sending NSNotifications across threads here.)
Timers are also handled by run loops. In contrast to run loops, you are likely to often be using timers directly in your program. The very easiest way of creating a timer is:
[self performSelector:@selector(aSelector) withObject:nil afterDelay:1.0];
but sometimes you want to create and manage NSTimer objects yourself, for example to be able to cancel and re-use a timer.
An NSTask is used to run another program as a subprocess of the current one. It’s a bit similar to starting a separate thread, but if a subprocess crashes, your main program will keep running. Communication between programs is also very different from communication between several threads in the same process.
You tagged your question with “iphone”, and on the iPhone you will never be using NSTasks.
NSOperations are used when you need to handle a larger amount of different tasks, placing them in queues and/or processing them in separate threads (although they don’t have to run in separate threads). If your application needs to create just a few, specialized threads, then there is no reason to use the NSOperation class. But if you will routinely generate tasks (like communicating with a server) that must be kept track of, NSOperation and NSOperationQueue will come in handy.
NSTimeris a timer object, a way to invoke a selector on an object in the future.
NSThreadis a thread class. I suppose you know what a thread is.
NSTaskis a process class, a way to run another program from your program.
NSOperation(I’m adding to the question) is a very nice abstraction for a single task. You embed your operation in this class and you can easily execute concurrently by means of an
NSRunLoopis the most difficult to understand. In some way abstracts and adapts the
select()unix system call, managing input sources and dispatching events and timers on a thread.
The guideline is the Apple Threading Programming Guide.
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
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
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.
- What are the new type qualifiers introduced with ARC?
- Implementing NSCopying in Subclass of Subclass
- How can I check what is stored in my Core Data Database?
- How do i set a modal segue (programmatically) to a push segue
- How to properly connect to Google Cast device and cast an url with an custom receiver?
- Do Swift inner classes have access to self of outer class?
- Uiscrollview lazy loading
- Is there any documentation to how to write custom Xcode Project Templates?
- libxml/tree.h no such file or directory
- Round two corners in UIView
- Swift optional chaining doesn't work in closure
- Can the url for the “Add to home screen” on iPhone Safari be customized?
- Perform Segue from UICollectionViewCell Button different from Cell Click
- NSError and __autoreleasing
- Switch between different Views in SWIFT