HackingWithSwift
HackingWithSwift copied to clipboard
Access UI properties from background thread in project 9
Hi All,
I thought you are not supposed to access/change UI. However I've noticed in this sample, fetchJSON is executed in the background thread but is reading UI property navigationController?.tabBarItem.tag. Am I missing something?
@objc func fetchJSON() { let urlString: String if navigationController?.tabBarItem.tag == 0 { urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100" } else { urlString = "https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100" } if let url = URL(string: urlString) { if let data = try? Data(contentsOf: url) { parse(json: data) return } } performSelector(onMainThread: #selector(showError), with: nil, waitUntilDone: false) }