Hello. I have a piece of program where I need to pass an array to two async queues and at the end of execution of that queues I need to synchronize that array with the main queue. But I get the error Data race in Runner.Forum.run() -> () at 0x7b0800029860 and I absolutely don't know how to do it with out the error. I attached the code
struct Forum {
let queue1 = DispatchQueue(label: "Pollination")
let queue2 = DispatchQueue(label: "Return to the hive")
struct Bee { let name: String }
struct Swarm { var bees = [Bee]() }
func run() {
let bee1 = Bee(name: "1")
let bee2 = Bee(name: "2")
let bee3 = Bee(name: "3")
let bee4 = Bee(name: "4")
var swarm = Swarm()
var copy2 = swarm
var copy1 = swarm
queue1.async {
copy1.bees.append(bee1)
copy1.bees.append(bee2)
print("queue1: \(copy1)")
swarm = copy1 // need synchronies the result of that queue
}
queue2.async {
copy2.bees.append(bee3)
copy2.bees.append(bee4)
print("queue2: \(copy2)")
}
let _ = readLine() // need press button and Enter here
print("after swarm: \(swarm)") // Data race in Runner.Forum.run() -> () at 0x7b0800029860
print("after copy1: \(copy1)")
print("after copy2: \(copy2)")
}