About IOS Runloop

Source: Internet
Author: User

First, what is Runloop?

Runloop is a running loop

Basic function: 1, keep the program running continuously 2, the processing of various events in the app (such as touch events, timer events, selector events) 3, save CPU resources, improve program performance: When doing things, the rest of the rest.

Second, the basis of runloop

Each thread has a unique Runloop object corresponding to it.

The runloop of the main thread has been automatically created, and the runloop of the child threads need to be actively created

Runloop is created at the first fetch and destroyed at the end of the thread.

2.1 Getting Runloop objects

1. Foundation

[Nsrunloop Currentrunloop]; Gets the Runloop object for the current thread

[Nsrunloop Mainrunloop]; Get the Runloop object for the main thread

2. Core Foundation

Cfrunloopgetcurrent (); Gets the Runloop object for the current thread

Cfrunloopgetmain (); Get the Runloop object for the main thread

2.2 Core Foundation 5 classes on Runloop

Cfrunloopref, Cfrunloopmoderef, Cfrunloopsourceref, Cfrunlooptimerref, Cfrunloopobserverref

2.2.1 Cfrunloopmoderef represents the operating mode of Runloop

A runloop consists of several mode, each of which contains several source/timer/observer

Each time the Runloop is started, only one mode can be specified, and this mode is called CurrentMode

If you need to switch mode, you can only exit loop and then reassign a mode to enter

This is done mainly to separate different groups of source/timer/observer, so that they do not affect each other

    • The system registers 5 mode by default:
    • The default mode of the Kcfrunloopdefaultmode:app, usually the main thread is run in this mode
    • Uitrackingrunloopmode: interface Tracking Mode for ScrollView tracking touch swipe to ensure that the interface is not affected by other mode when sliding
    • Uiinitializationrunloopmode: The first Mode entered when the APP was started, no longer used when the boot is complete
    • Gseventreceiverunloopmode: Accepts internal Mode for system events, usually with no
    • Kcfrunloopcommonmodes: This is a placeholder mode, not a real mode.
    • Cfrunloopsourceref is the event source (input source)
    • Previous sub-Methods
    • Port-based Sources
    • Custom Input Sources
    • Cocoa Perform Selector Sources
    • The present division method
    • SOURCE0: Not Port-based
    • Source1: Port-based
    • Cfrunlooptimerref is a time-based trigger
    • Basically, that's nstimer.
    • CFRUNLOOPOBSERVERREF is the Observer, able to monitor the state change of Runloop
    • There are several points of time that can be monitored

Third, Runloop processing logic

About IOS 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.