MBProgressHUD需要在主线程上访问

时间:2018-10-08 21:05:51

标签: ios swift mbprogresshud

在使用MBProgessHUD时出现此错误。我试图显示“正在加载指示器”,然后我的应用正在从Api加载数据,然后停止并随后将数据显示在屏幕上。

func getJson(){ 
        let spinningWheel = MBProgressHUD.showAdded(to: self.view, animated: true)
        spinningWheel.label.text = "Loading"
        spinningWheel.detailsLabel.text = "Please wait a moment.."
.
. 
.
.
.
.
. 
DispatchQueue.main.async {
                    self.movieName.isHidden = false
                    self.movieName.text = self.movieNameLabel
                    self.movieDescription.text = self.movieDescriptionLabel
                    self.movieImage.downloadedFrom(url: url2!)
                    spinningWheel.hide(animated: true)
                }

2 个答案:

答案 0 :(得分:0)

在iOS中,所有UIKit方法调用必须在主线程上进行。将所有UIkit用法包含在DispatchQueue.main.(a)sync调用中,如下所示:

func getJson() {
  DispatchQueue.main.async {
    let spinningWheel = MBProgressHUD.showAdded(to: self.view, animated: true)
    spinningWheel.label.text = "Loading"
    spinningWheel.detailsLabel.text = "Please wait a moment.."
  }
  // actually get JSON
}

答案 1 :(得分:0)

一个可疑的是您是从后台线程向该vc提供/推送/搜索的,所以

let spinningWheel = MBProgressHUD.showAdded(to: self.view, animated: true)

将在其中运行,因此请确保在主线程中进行导航,以便vc生命周期位于正确的位置