Runloop
Runloop is an implementation of the event receiving and distribution mechanism. Is part of the thread-specific infrastructure. A Runloop is an event processing loop that continuously dispatches work and processes input events. The purpose of using runloop is to let your thread
The main purpose of RunLoop
If there is no RunLoop, UIApplicationMain will return directly after execution. This means that the program is started and then finished. It is busy when there is work and dormant when there is no work.
When to use Runloop
Runloop is used only when you need to interact with the thread
Runloop Mode
A Runloop may have several modes
A Runloop Mode is actually a collection of sources, timers, and observers. Different modes isolate different groups of sources, timers, and observers. Runloop can only run in one Mode at a time, handling the Source, Timer, and Observer in that Mode.
There are five modes mentioned in apple documentation, which are:
-
NSDefaultRunLoopMode: default mode, normally in this mode
-
NSConnectionReplyMode
-
NSModalPanelRunLoopMode
-
NSEventTrackingRunLoopMode: use this Mode to track events from the user interaction (such as a UITableView slide up and down)
-
NSRunLoopCommonModes
The only publicly exposed modes in iOS are NSDefaultRunLoopMode and NSRunLoopCommonModes. NSRunLoopCommonModes is actually a collection of the Mode, including NSDefaultRunLoopMode and NSEventTrackingRunLoopMode by default.
Source
Events that wake up the Runloop. For example, when the user clicks on the screen, an input source is created.
source0
: Non-system event
Contains only one callback (function pointer), which does not actively fire events. To use this, you need to call CFRunLoopSourceSignal(source), mark the source as pending, and then manually call CFRunLoopWakeUp(runloop) to wake up the Runloop to handle the event.
source1
: System event
Contains a mach_port and a callback (function pointer) that is used to send messages to each other through the kernel and other threads. This Source actively wakes up the RunLoop thread
Timer
NSTimer, which we often use, falls into this category.
Observer
An observer can listen for runloop state changes and react to them.
RunLoop Runs the process
Classic a larger version
When nothing is going on, the Runloop goes to sleep. When the external source wakes it up, it processes the received timer/source in turn, and then goes to sleep again.
Common interview questions:
Runloop
How does it relate to threads?
Each thread has a unique corresponding RunLoop object whose relationship is stored in a global Dictionary; The RunLoop for the main thread is created automatically, and the RunLoop for the child thread needs to be created actively. The RunLoop is created on the first fetch and destroyed at the end of the thread
Runloop
What is the function of mode?
Specifies the priority of the event in the run loop.
Threads need different modes to run, to respond to different events, to handle different situational patterns. (For example, when you can optimize tableView, you can set UITrackingRunLoopMode to not perform some operations, such as setting pictures, etc.)
In order to+scheduledTimerWithTimeInterval:
Is triggered in a waytimer
, while swiping the list on the page,timer
Will pause the callback, why?
When you swipe scrollView, the RunLoop of the main thread will switch to the Mode UITrackingRunLoopMode, which also performs tasks under UITrackingRunLoopMode. However, timer is added under NSDefaultRunLoopMode, so the timer task will not be executed. Only when the task of UITrackingRunLoopMode is executed and runloop switches to NSDefaultRunLoopMode, Before the timer continues.
How to solve the problem that timer will pause the callback when sliding a list on a page?
Put the Timer in NSRunLoopCommonModes and do it
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
Copy the code
NSTImer
What should I pay attention to when using it?
- Notice what mode should be set when timer is added to runloop
- Note that when the timer is not needed, the invalidate method must be called to invalidate the timer, otherwise the timer will not be released
RunLoop
What are the applications?
Resident memory, AutoreleasePool automatic release pool
AutoreleasePool
和 RunLoop
What’s the connection?
After the iOS application is launched, two Observers are registered to manage and maintain AutoreleasePool. Application has just started the default registered a lot of the Observer, there are two of the Observer the callout is _ wrapRunLoopWithAutoreleasePoolHandler, these two are and automatic release pool is related both to monitor.
-
The first Observer listens for the RunLoop to enter by calling objc_autoreleasePoolPush() to add a sentinel object flag to the current AutoreleasePoolPage to create the automatic release pool. The order of this Observer is -2147483647, the highest priority, ensuring that it occurs before any callback operation.
-
The second Observer listens for the RunLoop when it is asleep and when it is about to exit. When going to sleep, objc_autoreleasePoolPop() and objc_autoreleasePoolPush() are called to clean up from the newly added object until the sentry object is encountered. When exiting RunLoop, objc_autoreleasePoolPop() is called torelease objects in the pool automatically. The order of this Observer is 2147483647, which has the lowest priority and is guaranteed to occur after all callbacks.
NSRunLoop is different from CFRunLoopRef
CFRunLoopRef is based on C thread safety, NSRunLoop is based on CFRunLoopRef object-oriented API is not safe
note
Due to the author’s limited level, it is inevitable to make mistakes, if there are any questions, please feel free to comment.