I created the playground below to answer my question "If I created a class instance using DispatchQueue.global().async would that class remain in its own asynchronous queue? Even if the main app called one of that classes methods, would that method run asynchronously compared to the main app?
With the sleep line I discovered that the answer is "no."
But I am curious if there is a legit way to do this? Or even if there is, it is considered bad programming?
import UIKit
class MyFunx : NSObject {
var opsCount = 0
override init() {
super.init()
}
func addValues (a: Int, b: Int) {
let c = a + b
opsCount += 1
sleep(1)
}
}
var firstVar = 0
var secondVar = 0
var myFunx : MyFunx?
while secondVar < 100 {
print ("starting")
if myFunx == nil {
print ("making myFunx")
DispatchQueue.global().async {
myFunx = MyFunx()
}
} else {
myFunx!.addValues(a: firstVar, b: secondVar)
}
firstVar += 1
secondVar += 1
}
print ("myFunx = \(myFunx)")
print ("\(myFunx?.opsCount)")
print ("exiting")