HackingWithSwift icon indicating copy to clipboard operation
HackingWithSwift copied to clipboard

Access UI properties from background thread in project 9

Open at1993 opened this issue 3 years ago • 0 comments

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) }

at1993 avatar Jun 08 '22 13:06 at1993