Я хочу сделать цикл for с помощью async. Для имитации асинхронности я добавил несколько задержек.
import Cocoa
class ViewController: NSViewController {
private let semaphore = DispatchSemaphore(value: 0)
private let concurrentQueue = DispatchQueue.global()
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...10 {
if run(i) {
break
}
}
}
func run(_ i:Int) -> Bool{
let seed = arc4random()
let isSuccess = seed % 5 == 1
let delayInSeconds = Int(seed % 3)
concurrentQueue.asyncAfter(wallDeadline: .now() + .seconds(delayInSeconds)) {
DispatchQueue.main.async {
self.textView.string += "\(i)\t\(seed)\n"
}
self.semaphore.signal()
}
semaphore.wait()
return isSuccess
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBOutlet var textView: NSTextView!
}
Проблема в том, что при запуске кода все строки добавляются одновременно. Я хочу, чтобы он добавлялся один за другим. Есть идеи?