Introduction to iOS RunLoop and iosrunloop

Source: Internet
Author: User

Introduction to iOS RunLoop and iosrunloop
1. What is RunLoop?

RunLoop is a running loop.

Basic functions: 1. Keep the program running. 2. process various events in the App (such as touch events, Timer events, and Selector events). 3. save CPU resources and improve program performance: do things during work and rest during work.

 

 

 

 

Ii. Basics of RunLoop

Each thread has a unique RunLoop object.

The RunLoop of the main thread has been automatically created. The RunLoop of the sub-thread needs to be automatically created.

RunLoop is created when it is obtained for the first time and destroyed when the thread ends.

2.1 get the RunLoop object

1. Foundation

[Nsunloop currentRunLoop]; // obtain the RunLoop object of the current thread

[Nsunloop mainRunLoop]; // get the RunLoop object of the main thread

 

2. Core Foundation

CFRunLoopGetCurrent (); // get the RunLoop object of the current thread

CFRunLoopGetMain (); // obtain the RunLoop object of the main thread

 

2.2 five classes about RunLoop in Core Foundation

CFRunLoopRef, CFRunLoopModeRef, CFRunLoopSourceRef, CFRunLoopTimerRef, CFRunLoopObserverRef

2.2.1 CFRunLoopModeRef indicates the running mode of RunLoop

A RunLoop contains several modes, and each Mode contains several Source/Timer/Observer

Only one of the modes can be specified when RunLoop is started. This Mode is called CurrentMode.

If you need to switch the Mode, you can only exit the Loop, and then specify a new Mode to enter

In this way, the Source, Timer, and Observer of different groups are separated to avoid mutual influence.

  • Five modes are registered by default:
  • KCFRunLoopDefaultMode: the default Mode of the App. Generally, the main thread runs in this Mode.
  • UITrackingRunLoopMode: interface tracking Mode, used for ScrollView tracking touch sliding, ensure that the interface sliding is not affected by other modes
  • UIInitializationRunLoopMode: Enter the first Mode when the App is started.
  • GSEventReceiveRunLoopMode: Internal Mode for accepting system events, which is usually unavailable
  • KCFRunLoopCommonModes: This is a placeholder Mode, not a real Mode.
  • CFRunLoopSourceRef is the event source (input source)
  • Previous Division
  • Port-Based Sources
  • Custom Input Sources
  • Cocoa Perform Selector Sources
  • Current Division
  • Source0: Non-Port-based
  • Source1: Port-based
  • CFRunLoopTimerRef is a time-based trigger.
  • Basically, NSTimer
  • CFRunLoopObserverRef is an observer and can monitor the status change of RunLoop.
  • The following time points can be monitored:

 

Iii. RunLoop processing logic

 

Related Article

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.