Differences between nstimer, nstask, nsthread, and nsunloop:
- NstimerIt is a timer object, and method calling is a future choice object.
- NsthreadIs a Thread class. That is, to create a thread.
- NstaskClass is a process, one way to run a program from your other programs.
- NsoperationIs a very beautiful abstract task. You can easily embed your operations in this class at the same time by means of an nsoperationqueue execution class.
- NsunloopIn fact, the essence of the nsunloop is a message mechanism processing mode. In a sense, abstract and adapt to the selection of () UNIX system calls, management and scheduling of Input Source events and thread timers.
Each program runs on at least oneThread. You can think of every program execution thread in a separate process, and each other runs in parallel.
If you have some port-like user interfaces or other code that requires listening to network events, you needRunning cycle. Each nsthread automatically has its own running cycle, and you seldom directly care about themselves. The autorelease pool is also created and released in the running loop.
[View more comments about the autorelease pool. The most important thing to remember is that new threads must consider setting up an autorelease pool for care. For example, the method called with detachnewthreadselector should be their first and last rows as follows:
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; [code here] [pool release];
This is also applicable to technologies used by other threads.]
InMain thread, All the UI processing is happening, and the running loop is very important because it maintains the interface response. This is why the code that you should never run takes time in the main thread: it will eat all the threads and the running time of the Loop will not be allowed to run normally, the interface is locked or slow. If you need to execute time-consuming computing or keep a task running in the background, you should create a new thread. Similarly, you may not have to consider the formation of a new running loop. A Simple Method for executing in a new thread:
[NSThread detachNewThreadSelector:@selector(theSelector) toTarget:self withObject:nil];
Inter-thread communication may be very tricky. You should know the method of using the performselector: onthread: withobject: waituntildone: and using mselecw.mainthrEAD: withobject: waituntildone:
TimerAlso process the running cycle. On the contrary, you may often use your program timer to run the loop directly. The simplest way to create a timer is to use a very Timer:
[self performSelector:@selector(aSelector) withObject:nil afterDelay:1.0];
But sometimes you need to create and manage the nstimer object yourself, for example, you can cancel and re-use a timer.
OneNstaskUsed to run one or more plans. This is a bit like starting with an independent thread, but if the sub-crashes, your main program will continue to run. The communication between programs is also very different from that between multiple threads in the same process.
You use the "iPhone" for your problem and on the iPhone you will never use nstasks.
NsoperationsIs used when you need to process different tasks with a large volume, they put queues and/or process them in separate threads (although theyNoRun in a separate thread ). If your application needs to be created with only a few dedicated threads, there is no reason to use the nsoperation class. However, if you want to communicate with the server frequently, you must keep records. nsoperation and nsoperationqueue can be used.
Nsunloop:The first is part of the concept of run loop, which is used to loop and process events. Specifically, there are two aspects: 1. scheduled task startup (generally used in collaboration with timer); 2. event processing.
In a single-threaded app, you do not need to pay attention to the run loop, but it does not mean no. When the program starts, the system has added the run loop to the main thread. It ensures that our main thread is in a "Waiting" state after it is run (rather than running it once like some command line programs ), at this time, if an event is received (timer's scheduled time is reached or messages from other threads), the task will be executed; otherwise, the task will be in sleep state.
If we want to write multi-threaded programs, we may need to manage the run loop by ourselves.
The following describes the parameters in the method proposed by the author:
Runmode: nsdefaultrunloopmode, which can be understood as a "filter". We can only monitor the events we care about. Generally, nsdefaultrunloopmode is the most commonly used.
The run loop startup method is written by LZ, which is described as follows:
Runs the loop once, blocking for input in the specified mode until a given date.
Start the run loop once and wait for the input in a specific run loop mode.
If no input source, timer, or limitdate is appended, run loop will exit and no is returned.
Run loop is used in the following scenarios:
1. Use port or custom Input Source to communicate with other threads
2. Use timer in the thread (non-main thread)
3. Use the performselector... series (for example, performselecw.thread ,...)
4. Use threads to execute periodic work
The run loop does not need to be created. In the thread, you only need to call [nsunloop currentrunloop] to obtain
Suppose we want to wait for the callback of An Asynchronous Method. For example, connection. If the run loop is not started in our thread, it will not be effective (because the thread has finished running and Exits normally ).