3

Lets say i have an array of strings, and i call an async method that returns an int from it. I want to know when i have those int values in my array of ints.

let rndStrings = ["a", "b", "c"]
var rndInts = [Int]()
rndStrings.forEach { rndString in 
   someAsyncMethod { intResult in
     rndInts.append(intResult)
   }
}

I want to wait until rndInts has all 3 values

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Godfather
  • 4,040
  • 6
  • 43
  • 70
  • I am not sure , But if you want to _Know when your array is changed_ , you can maybe use `Property Observers` , maybe , which will be called every time you make changes to your array , so that means at the initialisation as well ! – Shubham Bakshi Oct 20 '18 at 07:45

1 Answers1

4

Don't wait. Get notified with DispatchGroup.

let rndStrings = ["a", "b", "c"]
let group = DispatchGroup()
var rndInts = [Int]()
rndStrings.forEach { rndString in 
   group.enter()
   someAsyncMethod { intResult in
     rndInts.append(intResult)
     group.leave()
   }
}
group.notify(queue: DispatchQueue.main) {
   print("finished")
}
vadian
  • 274,689
  • 30
  • 353
  • 361