Ios-run loop (timer)
When developing multi-threaded programs in ios, run loop is often encountered.
For run loop, there are very detailed descriptions on the Apple development website, www.bkjia.com
It is strongly recommended that you chew it off and do not be afraid of English.
Copy one piece:
Here is a simple example of Timer sources.
When the system creates a thread, a run loop is created by default, except that the default run loop of the main thread is run, other auxiliary threads have already created the run loop but have not run it.
Get the thread's run loop object
NSRunLoop* loop = [NSRunLoop currentRunLoop];
The preceding simple code can be used to obtain the run loop object of the current thread.
Create a timer
// Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doFireTimer:) userInfo:nil repeats:YES];
DoFireTimer is called every 0.1 seconds. Simple:
- (void) doFireTimer:(NSTimer *)timer{ NSLog(@doFireTimer, %f, timer.timeInterval);}
Start run loop
NSInteger loopCount = 2; do {// start the run loop of the current thread until it reaches the specified time, run loop processes all the data from input sources associated with the run loop. // For the input source associated with the current run loop in this example, only the source of the Timer type is available. // The Timer is updated every 0.1 second, send the trigger time to run loop, when the run loop detects this event, it will call the corresponding processing method (doFireTimer :) // because the observer is added in the run loop, and set observer to be interested in all run loop behaviors // when the runUntilDate method is called, observer detects that the run loop starts and enters the loop, and observer calls its callback function, when the behavior passed by the second parameter is kCFRunLoopEntry // The observer detects other behavior of the run loop and calls the callback function. The operation is similar to the above description [loop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.2]; // when the running time of the run loop is reached, the current run loop will be exited, and the observer will also detect the exit behavior of the run loop and call its callback function, the second parameter is passed through kCFRunLoopExit. -- loopCount;} while (loopCount );
Call the runUntilDate function. This function runs the current run loop for 0.2 seconds. Call twice in total.
Because the timer sends a message every 0.1 seconds, every runUntilDate should receive two messages. (If you change 0.2 to 0.21, it should be two times. If you change 0.2 to, it is not necessarily because when you send the second timer, the run loop lifecycle has just arrived, may not be processed for the second time ).
The general process is
1. The timer sends messages every 0.1 seconds.
2. run loop receives the message and calls the doFireTimer function.
Run the following command: The call stack on the left shows that doFireTimer is called from the run loop.
The complete code is as follows: you only need to start this thread in the main thread.
[NSThread detachNewThreadSelector: @ selector (MyThread) toTarget: self withObject: nil];
-(Void) doFireTimer :( NSTimer *) timer {NSLog (@ doFireTimer, % f, timer. timeInterval);}-(void) MyThread {nsunloop * loop = [nsunloop currentRunLoop]; // Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @ selector (doFireTimer :) userInfo: nil repeats: YES]; NSInteger loopCount = 2; do {// start the run loop of the current thread until it reaches the specified time, run loop processes all the data from input sources associated with the run loop. // For the input source associated with the current run loop in this example, only the source of the Timer type is available. // The Timer is updated every 0.1 second, send the trigger time to run loop, when the run loop detects this event, it will call the corresponding processing method (doFireTimer :) // because the observer is added in the run loop, and set observer to be interested in all run loop behaviors // when the runUntilDate method is called, observer detects that the run loop starts and enters the loop, and observer calls its callback function, when the behavior passed by the second parameter is kCFRunLoopEntry // The observer detects other behavior of the run loop and calls the callback function. The operation is similar to the above description [loop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.2]; // when the running time of the run loop is reached, the current run loop will be exited, and the observer will also detect the exit behavior of the run loop and call its callback function, the second parameter is passed through kCFRunLoopExit. -- loopCount;} while (loopCount );}