A brief talk on the understanding of Runloop

Source: Internet
Author: User

What is a runloop?

From the literal point of view is the operation of the cycle, in fact, the internal is the Do-while cycle, the loop within the constant processing of various tasks (such as Source,timer,observer)

One thread corresponds to a runloop, the runloop of the main thread is started by default, the runloop of the child thread is started manually (Run method)

Runloop can only select one mode to start, if there is no source,timer,observer in the current mode, then exit directly Runloop

Two how do you use Runloop in the development process? What application scenario?

Open a resident thread (let a child thread not go extinct, wait for messages from other threads, handle other events)

Turn on a timer in a sub-thread

Perform some long-term monitoring in a child thread

Can control the timer to run in a specific mode

Enables certain events (behaviors, tasks) to be performed in a specific mode

You can add observer to monitor the status of the runloop, such as listening for the processing of click events (such as doing some processing before all click events)

The following code is used to introduce the use of runloop in the thread

Our goal is to open up a sub-thread to handle some events and then put the other thing in the first thread to handle

#import "ViewController.h"

@interface Viewcontroller ()

@property (Nonatomic,strong) Nsthread*thread;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Uibutton*but1=[uibutton Buttonwithtype:uibuttontypesystem];

[But1 Setframe:cgrectmake (50, 50, 40, 30)];

[But1 Setbackgroundcolor:[uicolor Bluecolor];

[But1 addtarget:self Action: @selector (Text1) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:BUT1];

Uibutton*but12=[uibutton Buttonwithtype:uibuttontypesystem];

[But12 Setframe:cgrectmake (120, 50, 30, 30)];

[but12 Setbackgroundcolor:[uicolor Redcolor];

[But12 addtarget:self Action: @selector (TEXT12) forcontrolevents:uicontroleventtouchupinside];

[Self.view addsubview:but12];

}

-(void) Text1

{

NSLog (@ "%@", [Nsthread CurrentThread]);

Self.thread=[[nsthread alloc]initwithtarget:self selector: @selector (TEXT2) Object:nil];

[Self.thread start];

}

-(void) Text2

{

Get the current sub-thread corresponding to the Runloop, that is, the beginning of a process will open a Runloop object, the Runloop object for the child thread needs to be opened manually, you need to call the Run method;

Nsrunloop*runloop=[nsrunloop Currentrunloop];

NSLog (@ "2%@", [Nsthread CurrentThread]);

Runloop's role is to ensure the survival of the thread, the nstimer here is to ensure that runloop do not exit, if there is no Runloop mode of operation, the Runloop will automatically exit,

1 First method plus timer

Nstimer*timer=[nstimer timerwithtimeinterval:2 target:self selector: @selector (too1) Userinfo:nil Repeats:yes];

[Runloop Addtimer:timer Formode:nsdefaultrunloopmode];

//

2. Add a port to Runloop to ensure Runloop does not exit

[Runloop Addport:[nsport Port] Formode:nsdefaultrunloopmode];

[Runloop Run];

}

Continue to let the first thread continue to work after clicking on the second red button

-(void) text12

{

[Self performselector: @selector (text13) onThread:self.thread Withobject:nil waituntildone:yes];// In order to carry out other events in the previous thread, we did a live processing of the previous thread, so we used runloop to keep the thread alive and not let it out;

}

-(void) text13

{

NSLog (@ "13%@", [Nsthread CurrentThread]);

}

A brief talk on the understanding of Runloop

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.