Well, I forgot something.
The first one months think of the RUNLOOP study more thorough, but because there is no actual use in the project for the sake of, to now unexpectedly almost forget, I have to record it, and later forget I can also find back here to see.
Here is the test code:
-(void) viewdidload
{
[Super Viewdidload];
//Steal a lazy here, use Performselectorinbackground directly to create a thread and execute the Configrunloop method
[Self Performselectorinbackground: @selector (Configrunloop) Withobject:nil];
uibutton* __button1 = [UIButton buttonwithtype:uibuttontyperoundedrect];
[__button1 settitle:@ "Fire Event" forstate:uicontrolstatenormal];
//Trigger event start Runloop
[__button1 addtarget:self Action: @selector (Triggerevent) forcontrolevents:uicontroleventtouchupinside];
__button1.frame = CGRectMake (0, 0, 100, 80);
[Self.view Addsubview:__button1];
uibutton* __button2 = [UIButton buttonwithtype:uibuttontyperoundedrect];
[__button2 settitle:@ "Stop runloop" forstate:uicontrolstatenormal];
Auto-exit thread after//runloop cycle completes
[__button2 addtarget:self Action: @selector (Stoprunloop) forcontrolevents:uicontroleventtouchupinside];
__button2.frame = CGRectMake (110, 0, 120, 80);
[Self.view Addsubview:__button2];
}
-(void) stoprunloop{
_shouldstop = YES;
}
-(void) triggerevent{
if (cfrunloopiswaiting (_runloopref)) {
NSLog (@ "Runloop is waiting for event input");
//Add input Events
Cfrunloopsourcesignal (_source);
//wake-up thread, the thread wakes up and discovers that the event needs to be handled, and immediately handles the event
Cfrunloopwakeup (_RUNLOOPREF);
}else {
NSLog (@ "Runloop is handling events");
//Add input event, currently processing an event, processing the current newly entered event immediately after completion of the current event
Cfrunloopsourcesignal (_source);
}
}
Background events to be processed by this input source
static void Fire (void* info __unused) {
NSLog (@ "I'm working on background tasks Now");
Sleep (5);
}
-(void) configrunloop{
//This is already a child thread. Oh, not the main thread.
_tthread = [Nsthread CurrentThread];
//This is also the runloop of this sub-thread Oh
_runloopref = Cfrunloopgetcurrent ();
Bzero (&_source_context, sizeof (_source_context));
//Create an event-based source here
_source_context.perform = fire;
_source = cfrunloopsourcecreate (NULL, 0, &_source_context);
//Add the source to the current Runloop
Cfrunloopaddsource (_runloopref, _source, kcfrunloopcommonmodes);
while (!_shouldstop) {
NSLog (@ "Runloop start Running");
//Each runloop only run 10 seconds, every 10 seconds to do a test, if there is no need to deal with the background task, let this thread itself terminate, no violence kill
Cfrunloopruninmode (Kcfrunloopdefaultmode, ten, NO);
NSLog (@ "Runloop stop operation");
}
_tthread = nil;
}
Reprint iOS--->runloop