introduction RunLoop has always been a mysterious field. Many developers with 2 or 3 years of experience cannot accurately describe its function. In fact, RunLoop is not as mysterious as everyone imagines. This article will take you to analyze the "mysterious RunLoop" What is RunLoop Literally
cycle Basic Function
Existence Value No RunLoop With RunLoop Main run loop
RunLoop Object
Foundation NSRunLoop Core Foundation CFRunLoopRef
RunLoop Information
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
http://opensource.apple.com/source/CF/CF-1151.16/ RunLoop and Threads Each thread has a unique corresponding RunLoop object The RunLoop of the main thread has been automatically created, and the RunLoop of the child thread needs to be created actively RunLoop is created when it is first acquired and destroyed when the thread ends. Get the RunLoop object
RunLoop related classes
CFRunLoopRef CFRunLoopModeRef CFRunLoopSourceRef CFRunLoopTimerRef CFRunLoopObserverRef Note: RunLoop will exit directly if it does not have these things CFRunLoopModeRef
A RunLoop contains several Modes, and each Mode contains several Source/Timer/Observer Each time the RunLoop is started, only one of the modes can be specified, and this mode is called CurrentMode. If you need to switch modes, you can only exit the loop and then re-assign a mode to enter. This is mainly done to separate different groups of Source/Timer/Observer so that they do not affect each other. Related Classes The system registers 5 modes by default: (the first two and the last one are commonly used)
CFRunLoopSourceRef
Port-Based Sources (based on ports, interacting with other threads, through messages published by the kernel) Custom Input Sources Cocoa Perform Selector Sources (performSelector… method)
Source0: Non-Port-based Source1: Port-based Source0: event event, which only contains callbacks. You need to call CFRunLoopSourceSignal(source) first, mark this Source as pending, and then manually call CFRunLoopWakeUp(runloop) to wake up RunLoop. Source1: Contains a mach_port and a callback, which is used to send messages between the kernel and other threads, and can actively wake up the RunLoop thread. Function call stack Function call stack CFRunLoopTimerRef
CFRunLoopObserverRef
use
Special attention
RunLoop processing logic – Official version logic – Netizens’ edited version Netizen version Note: Before entering RunLoop, it will determine whether the mode is empty, and exit directly if it is empty RunLoop Application
1.NSTimer (most common RunLoop usage)
Scene restoration When dragging, the mode changes from NSDefaultRunLoopMode to UITrackingRunLoopMode At this time, it looks like the following: NSTimer no longer responds to the image rotation. NSDefaultRunLoopMode Both modes can run in NSRunLoopCommonModes mode At this time, it is as follows: NSTimer can run normally in both modes 2. ImageView Requirement: Do not display the image when the user is dragging (UI interaction), and display the image when the dragging is completed Method 1: Monitor UIScrollerView scrolling (monitor through UIScrollViewDelegate, no example here) Method 2 RunLoop Set the running mode
3.PerformSelector inModes: Set the operating mode 4. Resident thread (important) Application scenario: often perform time-consuming operations in the background, such as monitoring network status, scanning sandbox, etc., and do not want the thread to be destroyed after processing the event and keep it in a resident state *** Type (recommended) Open
Exit - Exit the current thread
The second method (weird method) Advantages: It is convenient to exit RunLoop - define a flag while(flag){…}
5. Autorelease pool Release before sleeping (kCFRunLoopBeforeWaiting), create a release pool before processing events, and the objects created in the middle will be put into the release pool Special Note: It is recommended to wrap it with @autoreleasepool {...} before starting RunLoop Significance: Create a large release pool to release temporary objects created during {}. Generally, good framework authors will do this.
Off topic: In order to improve the user experience in the future, we can put the operations that need to be done into NSDefaultRunLoopMode without event processing when the user interacts with the UI. Added: GCD timer The general NSTimer timer may be inaccurate due to RunLoop. As mentioned above, GCD is not affected by RunLoop. Let's briefly talk about its use.
RunLoop Interview Questions There are often interviewers who like to show off and ask about RunLoop during the interview. But does he really know how to use it? Maybe he himself doesn’t quite understand it. Below I will give a brief summary of the interview about RunLoop, which is also a summary of the whole article
Literally: running loop, running in circles In fact, it is a do-while loop inside, which continuously processes various tasks (such as Source, Timer, Observer) One thread corresponds to one RunLoop. The RunLoop of the main thread is started by default, and the RunLoop of the child thread needs to be started manually (call the run method) RunLoop can only select one Mode to start. If there is no Source, Timer, or Observer in the current Mode, then RunLoop will exit directly.
Start a timer in the child thread Do some long term monitoring in the child thread
*** The article I published before was not very complete. I spent two days to sort it out again. If there are any deficiencies, please point them out. I will update it as soon as possible. |
<<: Don't worry about MVC or MVP. Listen to me.
>>: Summary of Android methods to avoid memory overflow (Out of Memory)
No matter what kind of marketing we do, we need t...
Every year, Apple holds a major conference. WWDC ...
User visual fatigue, slow content iteration, and ...
This year's 618 is becoming more and more &qu...
How much does it cost to be an agent of Home Furn...
Beauty leans on beauty, what exactly is beauty le...
Speaking of soup Many people think All the nutrie...
Friends who have traveled to the Qinghai-Tibet Pl...
There are no eternal rivals in the automotive ind...
As we all know, colorful clothes are made through...
Recently, many places including Ningbo, Zhejiang ...
Suzhou rain gear applet production price 1. Displ...
2020 College Entrance Examination Chinese Composi...
Today we are going to talk about a common topic, ...