What is a message loop:
The message loop is the Nsrunloop class, and each thread has its own message loop.
The message loop of the main thread is turned on by default (need to detect events), and the child threads are turned off by default (usually do not require child thread detection events).
The purpose of the message loop:
Ensure that the program does not exit, handles input events (input source and timer source), and causes the program to hibernate if no events occur.
Two modes of operation for message loops: Nsdefaultrunloopmodel and Nsrunloopcommonmodels
The message loop needs to be matched in a certain pattern, and when a timing source is added to the message loop, the pattern of the message loop must match the pattern of the timing source to execute:
In the main thread:
Nstimer *timer= [Nstimer timerwithtimeinterval:1.0 target:self selector: @selector (Textdemo) Userinfo:nil Repeats:YES] ;
[[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];
/* When using the current mode Nsdefaultrunloopmode, when a visible event is triggered on the UI, the pattern changes and the method in the timing source does not run
When you change the mode to Nsrunloopcommonmodels, the methods in the timing source execute normally, because the mode contains two underlying patterns.
*/
A way to turn on the message loop in a child thread:
Turn on the message loop directly
[[Nsrunloop Currentrunloop] run];
When to open
[[Nsrunloop Currentrunloop]rununtildate:[nsdate datewithtimeintervalsincenow:2.0]];
Message loops in iOS