It is a very important concept in iOS
development RunLoop
. It provides an event loop mechanism for handling various events, such as user interaction, network requests, timers, etc. RunLoop
Not only is it iOS
one of the core in development, but it is also widely used in the development of other platforms. This article will introduce you to Swift
the RunLoop
basic concepts and usage methods.
What is RunLoop?
RunLoop
It is an event loop mechanism that is used to iOS
handle various events in applications. RunLoop
Running in the main thread of the application, it is responsible for managing events in that thread and ensuring that important tasks such as UI updates can be performed smoothly. RunLoop
Also responsible for handling events sent by other threads, such as network requests and so on.
RunLoop
The basic idea is to process events in a loop. When RunLoop
started, it enters an infinite loop waiting for an event to occur. When an event occurs, RunLoop
the corresponding processing method will be called to handle the event and continue to wait for the next event to occur. RunLoop
Will continue to run until manually stopped or the application exits.
RunLoop and threads
In iOS
, each thread has one RunLoop
, but by default, RunLoop
it is disabled. To use it RunLoop
, you must start it manually and add it to the thread’s run loop.
For example, to use it in the main thread RunLoop
, you can use the following code:
RunLoop.current.run()
This will start the main thread RunLoop
and enter an infinite loop waiting for events to occur.
RunLoop mode
RunLoop
Pattern is RunLoop
an important concept that defines RunLoop
the types of events that need to be handled during operation. A RunLoop can have multiple modes, but only one mode can be processed at any time. Each mode can contain multiple input sources (input source)
and timers (timer)
, and RunLoop
the processing method of the next event will be determined based on the input sources and timers in the current mode.
RunLoop
Several built-in modes are provided, such as:
NSDefaultRunLoopMode
:Default mode, handles allUI
events, timers andPerformSelector
methods.UITrackingRunLoopMode
: Tracking mode, only handles events related to interface tracking, such asUIScrollView
scroll events.NSRunLoopCommonModes
: Public mode, including bothNSDefaultRunLoopMode
andUITrackingRunLoopMode
.RunLoop
It also allows developers to customize the mode to meet specific needs.
timer
In iOS
development, timer is a common event, such as refreshing at regular intervals UI
, performing background tasks, etc. RunLoop
A timer mechanism is provided (timer)
to perform an operation within a specified time interval.
For example, to create a timer and start it in the main thread, you can use the following code:
let timer = Timer(timeInterval: 1.0, repeats: true) { timer in // } RunLoop.current.add(timer, forMode: .common)
This will create a timer that fires every 1 second and add it to the main thread in public mode RunLoop
.
When adding a timer, you need to specify RunLoop
the mode it belongs to. If mode is not specified, it defaults to NSDefaultRunLoopMode
. You can use this if you need to respond to timer events in multiple modes NSRunLoopCommonModes
.
input source
An input source (input source)
is a RunLoop
mechanism used with javascript to handle asynchronous events such as network requests, file reads and writes, and so on. RunLoop
During the running process, it will check whether there is an input source that needs to be processed in the current mode, and if so, it will be processed immediately.
The input source can be one Port、Socket、CFFileDescriptor
and so on. To use an input source, you must add it to RunLoop
and set a callback function to handle input events.
For example, to use the input source in the main thread, you can use the following code:
let inputSource = InputSource()
inputSource.setEventHandler {
//
}
RunLoop.current.add(inputSource, forMode: .common)
This will create an input source and add it to the main thread in public mode RunLoop
.
Perform Selector
Perform Selector
It is a way to call a method in RunLoop
which a method can be executed asynchronously. When calling a method, you can set the delay execution time and RunLoop
mode. This method will be executed at the specified time interval until canceled.
For example, to use it in the main thread Perform Selector
, you can use the following code:
RunLoop.current.perform(#selector(doSomething), target: self, argument: nil, order: 0, modes: [.default])
This will execute the method asynchronously in default mode doSomething
.
Common operations of RunLoop
In addition to the above basic operations, RunLoop
other commonly used operations are also provided, such as:
stop
: StoppedRunLoop
operation.runUntilDate
: RunRunLoop
until the specified date.runMode
: RunRunLoop
the event processing loop in the specified mode.currentMode
: GetRunLoop
the current running mode.
RunLoop and thread safety
In iOS
development, multi-threading is a common problem. RunLoop
When handling asynchronous events, thread unsafe issues may occur. To ensure RunLoop
thread safety, you can use the following methods:
- Used
RunLoopQueue
in a queueRunLoop
to perform asynchronous operations. - Use it in the main thread
RunLoop
to handle asynchronous events to avoid cross-thread operations.
in conclusion
RunLoop
It is iOS
a very important concept in development. It provides an event loop mechanism for handling various events. RunLoop
The basic idea is to process events in a loop. When an event occurs, RunLoop
the corresponding processing function will be called to handle the event. RunLoop
It also provides timers, input sources, and Perform Selector
other mechanisms to handle asynchronous events. Understanding RunLoop
the working principle can help us better understand iOS
the operating mechanism of the application and avoid some strange problems.
Finally, let’s take a look at RunLoop
some things to note:
- Don’t block in the main thread
RunLoop
, otherwise it will causeUI
lag. - Avoid using
RunLoop
thisNSDefaultRunLoopMode
mode because a large numberUI
of events will be processed in this mode, which may cause other events to not be processed in time. - During use
RunLoop
, you need to pay attention to thread safety issues.
RunLoop
It is an event loop mechanism through which we can easily handle various events and avoid some strange problems. In daily development, we need to use it frequently RunLoop
, so it is recommended that you practice more and master RunLoop
various usages.