Hi How to correctly implement such functionality: There is a function call (They are called in a loop)
test1(timeout: 10)
test2(timeout: 5)
test3(timeout: 1)
For me, the correct execution should be like this - first, the first function is executed in 10 seconds, then the second in 5 after the first is completed and similarly the third should start 1 second after the second
I tried using DispatchQueue and Timer, but nothing worked for me
func test1(timeout: int, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline:
DispatchTime.now() + timeout) {
print("finish1")
completion()
}
})
func test2(timeout: int, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline:
DispatchTime.now() + timeout) {
print("finish2")
completion()
}
})
func test3(timeout: int, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline:
DispatchTime.now() + timeout) {
print("finish3")
completion()
}
})
The start of the execution of functions depends on the time, and I need the functions to be executed sequentially as they were started and the time was responsible for the start of execution, but after her turn came
You may have got a bit confused, between Operations and OperationQueue.
Start simple:
let operation1 = BlockOperation {
Thread.sleep(forTimeInterval: 5)
print("Operation 1 completed")
}
let operation2 = BlockOperation {
print("Operation 2 completed")
}
operation2.addDependency(operation1)
let queue = OperationQueue()
queue.addOperation(operation1)
queue.addOperation(operation2)