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