Tuesday, October 13, 2026
HomeiOS Developmentios - How one can set a variable that runs on background...

ios – How one can set a variable that runs on background thread and must entry ui

[ad_1]

I’m fetching an information supply in background. There are 2 urls and I select it with a tabBar. To know which url I have to entry, I take advantage of navigationController?.tabBarItem.tag. However It throws an error of “navigationController should be used from major thread solely”. I’ve tried to wrap it with DispatchQueue.major.async but it surely did not work. Any repair or new strategy appreciated.

override func viewDidLoad() {
    tremendous.viewDidLoad()
    performSelector(inBackground: #selector(fetchJSON), with: nil)
}

@objc func fetchJSON() {
    let urlString: String
    
    if navigationController?.tabBarItem.tag == 0 {a
        urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
    } else {
        urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"
    }
    
    if let url = URL(string: urlString) {
        if let information = strive? Knowledge(contentsOf: url) {
            parse(json: information)
            return
        }
    }
    performSelector(onMainThread: #selector(showError), with: nil, waitUntilDone: false)
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments