Sequential execution of asynchronous functions. How?

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

Answered by robnotyou in 683689022

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)

I’m afraid I don’t really understand your question. Let’s start with this:

They are called in a loop

Does that means there’s some outer loop around the calls to test1(…) through test3(…)? If so, does that loop expect these calls to be done before it turns around the loop? Consider this:

for i in 1...3 {
    // A
    callMyTestFunctions()
    // B
}

What’s the time difference between points A and B? Is it 16 seconds, because that’s how long test1(…) through test3(…) take to complete? Or is it tiny, because test1(…) through test3(…) are running ‘in the background’.

Next, what’s the role of these timers? Do test1(…) through test3(…) do actual work, and the timers act as a timeout? Or is the goal simply to delay things?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Have you considered using Operations, and an OperationQueue?

Setting maxConcurrentOperationCount = 1 will execute your operations sequentially.

Thanks for the answer My English is not very good, but I will try to explain Forget the loop and simplify

There are several functions (there may be 2 or more of them) Each function has a time parameter (This is the time in seconds after which the function should start working) I could use sleep, but this does not work for me because the application hangs. Therefore I am using afteAsync (deadline).

Here's a good example of how I'm doing it

let operation = OperationQueue()
operation.maxConcurrentOperationCount = 1

operation.addOperation {

  DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
    print("1")
  }
   
  DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
    print("2")
  }
   
  DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
    print("3")
  }
   
}

in this case, all functions will be executed simultaneously after 10 seconds. And I need them to be executed sequentially with a pause specified in the function parameter

Accepted Answer

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)

robnotyou Thanks

Yes, it works, but with such an implementation I don't yet understand how to do it all dynamically. I can have 10 functions, and here's how to specify the dependence on the previous one, I don't know yet

operation4.addDependency(operation3)
operation3.addDependency(operation2)
operation2.addDependency(operation1)

Thanks anyway I will think in this direction

As I said earlier, setting maxConcurrentOperationCount = 1 will execute your operations sequentially.
So they will execute in the order you add them to the queue.
In that case, you don't need to set the dependencies.

Sequential execution of asynchronous functions. How?
 
 
Q