Message loops in iOS

Source: Internet
Author: User

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

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.