Im trying to understand the Dispatch Sync and Dispatch Async, I know that its executes in sync and async manner of GCD. But when i try the below code it gave me weird scenario.
I testing the below code in Playground and Sync block executed 3times and the async block gave the NSException.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let que = DispatchQueue.init(label: "testing")
// Executed 3 times
que.sync {
for i in 0...10 {
print(i)
}
}
// Giving me NSException
que.async {
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
print("Label Added to Text View")
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Why it executes 3 times of sync block. and why there is an NSException error.