-1
var turn = 0
 for pos in self.history {

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .microseconds(Int(1000000 * turn))) {
        //////////
        //put stone in board
        ////////////
    }

    turn += 1
}

I use DispatchQueue.main.asyncAfter method to set stones with exact interval time in the board.

it works well with small stones.(1 stone per 1 sec) but more and more stones in the board, it becomes not working exactly.(2 stones per 2 sec: 2 stones are put simultaneously)

why this error happens?

JKU
  • 123
  • 1
  • 8

1 Answers1

3

It's right there in the method name-- "asyncAfter". The method isn't guaranteed to run code at a specific time, it runs code after a specific time. That might be immediately after the time or it might be later on. You should think of the method as meaning "wait at least this long", not "run code at exactly this time".

If you need more precise timing, consider using Timer or else creating a DispatchSourceTimer.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170