Modifications to the > layout engine must not be performed from a background thread after it has been accessed from the main thread

后端 未结 3 858
渐次进展
渐次进展 2020-12-15 16:52

I just downloaded iOS 13 for my iPhone and the new Xcode. I wanted to test my app for iOS 13, but when I try to run my app it will give me a error after a few seconds. Error

相关标签:
3条回答
  • 2020-12-15 17:08

    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.

    0 讨论(0)
  • 2020-12-15 17:22

    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()
    }
    
    0 讨论(0)
  • 2020-12-15 17:23

    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.

    0 讨论(0)
提交回复
热议问题