The answers to the interview questions are all about the basics of iOS

As a developer, it’s especially important to have a learning atmosphere and a networking community, and this is one of my iOS development networking groups:130 595 548, whether you are big ox or small white are welcome to enter, let us progress together, common development! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)

1. What is RunLoop?

  • Just as the name suggests: 1. Run a loop, a loop that does something while the program is running. NSRunLoop √ Core Foundation: CFRunLoopRef 3, NSRunLoop and CFRunLoopRef all represent RunLoop objects. 4. NSRunLoop is a layer of OC wrapper based on CFRunLoopRef. 5

  • The basic functions of RunLoop are as follows: 1. Keep the program running continuously; 2. Handle various events (such as touch events and timer events) in the App; 3

  • CFRunLoopModeRef √ CFRunLoopModeRef Indicates the operating Mode of RunLoop. √ A RunLoop contains several modes. Each Mode and contains several Source0 / Source1 RunLoop start/Timer/Observer) can only choose one of the Mode, if you need to switch Mode as currentMode), can only exit from the current Loop, Again to choose a different Mode to enter) group of Source0 / Source1 / Timer/Observer can be separated, if each other) Mode without any Source0 / Source1 / Timer/Observer, √ kCFRunLoopDefaultMode (NSDefaultRunLoopMode) : the default Mode of the App, in which the main thread usually runs √ UITrackingRunLoopMode: Interface tracking Mode, used for ScrollView tracking touch sliding, to ensure that the interface sliding is not affected by other modes

  • Application scope: √ Timer, PerformSelector √ GCD Async Main Queue √ Event response, gesture recognition, interface refresh √ Network request √ AutoreleasePool

  • Running applications √ Control thread life cycle (thread survival) √ Fix NSTimer stopping when sliding √ Monitor application lag √ Optimize performance

2, RunLoop and thread relation?

  • Each thread has a unique RunLoop object corresponding to it
  • Runloops are stored in a global Dictionary, with threads as keys and runloops as values
  • The thread is created without a RunLoop object; the RunLoop is created the first time it gets it
  • The RunLoop is destroyed at the end of the thread
  • The RunLoop for the main thread is automatically acquired (created), and RunLoop is not enabled for the child thread by default

What is the running logic of RunLoop?

3.1 Analysis of various events

  • Source0 touch event handling performSelector: onThread:

  • Source1 System event capture for inter-thread communication based on Port

  • Timers NSTimer performSelector:withObject:afterDelay:

  • Observers for listening for state UI refreshes of runloops (BeforeWaiting) Autorelease pool (BeforeWaiting)

01, notifying them that they are about to deal with Timers 02, notifying them that they are about to deal with Timers 03, notifying them that they are astounded: 30, If there is any Source1, go to step 8. Notify Observers that they are about to start resting, waiting for a message to be astounded. GCD Async To Main Queue 03> Source1 09, processing Blocks 10, 30, Notify Observers: Observers are about to exit Loop

As a developer, it’s especially important to have a learning atmosphere and a networking community, and this is one of my iOS development networking groups:130595548, whether you are big ox or small white are welcome to enter, let us progress together, common development! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)