Ios-run Loop (timer)

Source: Internet
Author: User

When developing multi-threaded threads in iOS, you often run into run loops.

For Run loop, the Apple development website has very detailed instructions, https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ Multithreading/runloopmanagement/runloopmanagement.html

It is highly recommended to chew down and not be afraid of English.

Copy a piece:


Here is a simple example of a 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 running, and the other worker threads have already created run loop but are not running.


Gets the run loop object for the thread

    nsrunloop* loop = [Nsrunloop Currentrunloop];
You can get the run loop object of the current thread by using the simple code above.


Create a Timer

    Creates and returns a new Nstimer object and schedules it on the current run loop in the default mode    [Nstimer sche duledtimerwithtimeinterval:0.1 target:self selector: @selector (dofiretimer:) Userinfo:nil Repeats:yes];
Dofiretimer is called once every 0.1 seconds. Simple as follows:

-(void) Dofiretimer: (Nstimer *) timer{    NSLog (@ "Dofiretimer,%f", Timer.timeinterval);}

start the Run loop

    Nsinteger loopcount = 2;    do {        //start the run loop of the current thread until the specified time, and run loop will process all data from input sources that are contacted by the run loop while running        For this example, the input source associated with the current run loop has only a timer type of source        //The timer sends the trigger time every 0.1 seconds to run Loop,run Loop detects this event by invoking the appropriate processing method (Dofiretimer:)        //due to the addition of observer in the run loop, and the setting observer is interested in all the run loop behaviors        // When the Rununtildate method is called, Observer detects that the run loop is started and enters the loop, observer calls its callback function, and the second argument passes the behavior when kcfrunloopentry        //Observer detects the run The other behavior of loop and invoking the callback function is similar to the description above        [loop rununtildate:[nsdate datewithtimeintervalsincenow:0.2]];        When the run time of run loop arrives, it exits the current run Loop,observer also detects the exit behavior of the run loop, calls its callback function, and the second argument passes the behavior of Kcfrunloopexit.        --loopcount;    } while (Loopcount);

Call the Rununtildate function, which causes the current run loop to run for 0.2 seconds. This is called a total of 2 times.

Because the timer sends a message every 0.1 seconds, it should receive 2 messages per Rununtildate. (change 0.2 to 0.21 what, should be 2 times, 0.2 words is not necessarily, because when the second timer is sent, the run loop life cycle has just arrived, may not be able to be processed the second time).

The general process is

1. The timer sends the message every 0.1 seconds

2. Run loop receives the message, calling the Dofiretimer function.

Run as follows: You can see from the left call stack that Dofiretimer is called from the run loop.


The complete code is as follows: Just start the thread inside the main thread.

[nsthreaddetachnewthreadselector:@selector(MyThread) totarget:selfwithobject: nil];

-(void) Dofiretimer: (Nstimer *) timer{NSLog (@ "Dofiretimer,%f", Timer.timeinterval);}        -(void) mythread{nsrunloop* loop = [Nsrunloop Currentrunloop]; Creates and returns a new Nstimer object and schedules it on the current run loop in the default mode [Nstimer sched    uledtimerwithtimeinterval:0.1 target:self selector: @selector (dofiretimer:) Userinfo:nil Repeats:yes];    Nsinteger Loopcount = 2; Do {//starts the run loop of the current thread until the specified time, and when run, running, the run loop processes all data from the input sources associated with the run loop//For this example and when        The input source contacted by the previous run loop only has a timer type of source//the timer sends the trigger time every 0.1 seconds to the run Loop,run loop to invoke the appropriate processing method when the event is detected (Dofiretimer:) Because Observer is added to the run loop and the settings observer are interested in all the run loop behavior//When the Rununtildate method is called, Observer detects that the run loop is started and enters the loop, observe R calls its callback function, and the second argument passes the behavior when KCFRUNLOOPENTRY//Observer detects other behavior of the run loop and invokes the callback function similar to the one described above [loop RUNUNTILDATE:[NSD        Ate datewithtimeintervalsincenow:0.2]]; When the run time of the run loop arrives, it exits the current run Loop,observerThe sample detects the exit behavior of the run loop and invokes its callback function, and the second argument passes the behavior of Kcfrunloopexit.    --loopcount; }while (Loopcount);}



Ios-run Loop (timer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.