iOS tamping: runloop

Source: Internet
Author: User

Objective: To summarize runloop and its impact on our daily development in a concise and understandable language.

1. What is Runloop

Runloop is a thread-related mechanism that can be simply understood as a loop. Wait for the event in this loop, and then handle the event. And this loop is thread-based. Each thread in the cocoa has its runloop. By runloop Such a mechanism, the thread is able to rest when there is no event to be processed and to run when there is a thing. Reduce CPU pressure.

2. Runloop in the daily development

After a simple understanding of runloop, we found that in fact, our usual development, behind all the runloop related.

But we're lucky we don't have to waste our time on these underlying details, and most of the work is given to the operating system for us. So about Runloop, what do we need to know and do if we don't want to be surrounded by the underlying details?

      1. Need to know Runloop's pit:

        • Nstimer
          in daily development, our contact with Runloop may have been through Nstimer recently. A timer can only be added to one runloop at a time. When we use it everyday, we usually add it to the default mode of the current runloop.

          refers to mode, and you need to talk about Runloop Modes
          Simply put, Runloop has multiple mode,runloop that can only run an event where the Mode,runloop only handles its current mode.

          So it leads to some places we need to be aware of. The

          • General timer is run on the default mode of Runloop, while ScrollView runloop the main thread to Uitrackingrunloopmode when the user slides. And this time, the timer will not run, method can not get fire.

          Use a real-world example to illustrate (own lesson):

          At the time of writing a registration interface, the user clicks to send the verification code, uses the timer, the countdown 60 seconds to allow the user to request to send the verification code again, and updates the interface seconds information every second. At this point, the timer runs on the default mode of the main thread. If the user slides the display at this time, the timer will be invalid, the interface can not update the situation. This is because of the runloop mode reason. The same is true for

        • Nsurlconnection,nsstream, which is run by default mode.

      2. Solution:

        • First: Set Runloop Mode, for example Nstimer, we specify that it runs on Nsrunloopcommonmodes, which is a M A collection of Ode. After registering to this mode, the event can be executed regardless of which mode the current Runloop is running.
        • Second: Another way to fix a timer is to execute and process the timer event on another thread, and then update the UI on the main path.

iOS tamping: 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.