Modifications to the > layout engine must not be performed from a background thread after it has been accessed from the main thread
Your network fetch code is almost right - you are reloading the table on the main queue but not stopping the activity indicator.
You just need to move that line inside the closure:
let downloadedData_user = try decoder.decode(Top_us.self, from: data) self.Top_Search = downloadedData_user.users DispatchQueue.main.async { self.tableView.reloadData() self.Indicator.stopAnimating() }
Note that by convention, properties should start with a lower case letter while classes should start with an upper case letter. Both should use camelCase, so Top_Search
should be topSearch
, Top_us
should be TopUsers
and Indicator
should be indicator
.
For anyone having problems pinning down the cause, try the following.
Click on your scheme (next to the stop button), then Edit Scheme.
Under Run -> Diagnostics you have to activate Main Thread Checker.
As indicated by the breakpoint that got added, your execution will now halt when you try to alter the UI on a non-main thread.
You should wrap handling error by DispatchQueue.main.async{}
func Download_ID() { let urlString = "https://www.instagram.com/\(self.username_String)/?__a=1" guard let url = URL(string: urlString) else { return } URLSession.shared.dataTask(with: url) { data, urlResponse, error in guard let data = data, error == nil, urlResponse != nil else { // Add DispatchQueue DispatchQueue.main.async { print(error) } return } do { let decoder = JSONDecoder() let downloadedData_user = try decoder.decode(Website.self, from: data) // your logic DispatchQueue.main.async { // update or reload table in here } } catch { // Add dispach_queue DispatchQueue.main.async { print(error) } } }.resume()}