In the process of developing software, we not only need to perform operations when the user clicks, but sometimes we also need to automatically perform some operations at regular intervals. For example, the most common opening screen advertisement countdown is closed, as well as some animation effects. There are generally two ways to implement these: one is to use a timer, and the other is to use asynchronous operations.
First we need to create a new Timer and use the following method to create it. The last .autoconnect()
is used to automatically start the connection timer. We can also use .connect()
to manually activate the connection.
let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
Here are some parameters:
every:
: This parameter is used to control how often the Timer is updated, in seconds. The 3
here means 3 seconds.on:
: This indicates when to activate this Timer. The .main
here means that it will be activated when the main thread is running (that is, it will be activated when the app starts running). There is also .current
which means it is activated when the current thread is running (that is, it means it is activated when the current view interface is displayed). It is best not to activate a timer on one thread on another thread, as this may lead to erroneous results.in:
: This is the loop mode of Timer. There are .common
, .default
and .tracking
. .common
contains a pseudo-mode for one or more other run loop modes. When using this mode to add an object to a run loop, the run loop monitors the object when running in any of the common modes. For more information about adding a run loop mode to the common mode set, see CFRunMode. .default
is set to the mode for processing input sources other than the connection object, which is the most commonly used run loop mode. .tracking
Mode set when tracking in a control, you can use this mode to add a timer that fires during tracking.It should be noted that if you are developing on macOS, there will be two more modes: .eventTracking
and .modalPanel
. .eventTracking
is the mode set when tracking events modally, such as a mouse drag loop. .modalPanel
is the mode set when waiting for input from a modal panel (such as saving or opening a panel).
Then close and disconnect the Timer as follows:
self.timer.upstream.connect().cancel()
Timer looks complicated, but you don’t need to think too much about using it. Next, let’s talk about how to use Timer after creating it.
In SwiftUI, we can let the view perform some operations when some data changes by using .onReceive()
. The specific method is as follows:
.onReceive(timer) { _ in
print("Timer active, add 1 second")
}
Then write a simple timer interface that counts down from 5 and loops again after the countdown:
struct TimerView: View {
@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var countNum = 5
var body: some View {
Text("\(countNum)")
.onReceive(timer) { input in
if countNum > 0{
countNum -= 1
} else if countNum == 0{
countNum = 5
}
}
}
}
Next, let’s talk about the asynchronous operation DispatchQueue
. This is also very commonly used, especially when you need to load data or do effects or animations. It is very easy to use.
How to use it:
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
print("The wait is over!")
})
Introduce the parameters:
deadline:
: When to perform the operation, .now() + .seconds(1)
here means waiting for 1 second starting from the current time.execute:
: What operation needs to be performed.This is relatively simple, so I won’t introduce it in too much detail.
I hope these will help someone in need~