Ios-runloop message loop

Source: Internet
Author: User

What is Runloop?

-runloop is the message loop, with a message loop inside each thread. -Only the main thread's message loop is turned on by default, and the child thread's message loop is not turned on by default.The purpose of Runloop-Ensure that the program does not exit.    -Responsible for handling input events. -If no event occurs, the program goes to hibernation.Event TypeInput Sources (source) & Timer Sources (timing source)

-Input source can be keyboard mouse, Nsport, nsconnection and other objects, timing source is Nstimer event

adding messages to the Loop

-Create an input source. (Take Nstimer as an example)

-Specifies the mode in which the event (source) runs in a loop, and joins the loop.

[Self performselector: @selector (demo2) onthread:thread Withobject:nil Waituntildone:no]; [[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];

    

Cycle common patterns

nsdefaultrunloopmode--default mode

nsrunloopcommonmodes--Normal Mode

    // 1.NSTimer Timing Source    Nstimer *timer = [Nstimer timerwithtimeinterval:1  target:self selector: @selector (Demo) Userinfo:nil Repeats:yes];     // 2. Add the timer source to the current thread's message loop    /*      1. Timing source     2. Mode      Nsdefaultrunloopmode Touch interface->timer stop-hand release->timer run     Nsrunloopcommonmodes touch Interface not affected     Touch interface kcfrunloopdefaultmode->uitrackingrunloopmode-> Hand Release Kcfrunloopdefaultmode     *    /[[Nsrunloop Currentrunloop] Addtimer:timer formode: Nsrunloopcommonmodes];     // 3. Messages can run when the pattern of the message loop matches the pattern of the message

Summarize

1. Create a message

2. Put the message into a loop and specify the mode in which the message runs

3. When matching the loop pattern, the message runs

Message loops in a child thread

// message Loops    for child threads Object : nil];    [Thread start];     // input source    /*      1. Method     2. Thread     3. Parameter     4. Whether to wait for the     completion of the method *    / / Create an input source, Message loops joined to child threads     // only the main thread's message loop is turned on by default, and the child thread's message loop is not turned on by default. --Manually turn on the message loop for the child thread    [self performselector: @selector (talk) Onthread:thread Withobject:nil Waituntildone:no ];
-(void) demo{NSLog (@"Demo is running");//NSLog (@ "%@", [Nsrunloop Currentrunloop].currentmode); //to manually turn on a message loop for a child thread//The 1.run method is turned off, OC.[[Nsrunloop Currentrunloop] run]; //2. Duration of the specific implementation of the uncertainty method and other factors//[[Nsrunloop Currentrunloop] rununtildate:[nsdate datewithtimeintervalsincenow:1]]; //3.apple Recommended (Learn)//BOOL shouldkeeprunning = YES; //Global//nsrunloop *therl = [Nsrunloop currentrunloop];//while (shouldkeeprunning && [Therl runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]); 
    NSLog (@"OK");

Ios-runloop message loop

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.