Using the below playground, when I print in the async I would assume the counts should be the same, but they are not. It seems that swift does not properly use inout asynchronously, but I need to be able to update an inout reference in an asynchronous call. Any idea how to make this work?
import XCPlayground
import Foundation
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
class MyObject {
var name:String!
var id:Int!
init(name:String, id:Int){
self.name = name
self.id = id
}
}
struct TempStorage {
static var baseObjs:[MyObject] = []
}
class myUtils {
private static func myFunc2(inout myObjs:[MyObject]){
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let newObj = MyObject(name: "BOB", id: 1)
myObjs = [newObj]
print(myObjs.count)
print(TempStorage.baseObjs.count)
}
}
static func myFunc(inout myObjs:[MyObject]) {
myUtils.myFunc2(&myObjs)
}
}
var baseObj = MyObject(name:"SUSY", id:0)
print(TempStorage.baseObjs.count)
myUtils.myFunc(&TempStorage.baseObjs)