首页 > 解决方案 > 当我尝试从 firebase 获取图像时,T​​ableview 崩溃

问题描述

我收到此错误。我还提供了一个屏幕截图,更详细地查看了我以前从未见过的错误

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在从主线程访问布局引擎后,不得从后台线程对其进行修改。”

在此处输入图像描述

   //MARK: EXPLANATION (your passing item in down below because this allows you to call whatever is in the item class)

func generateCellForITEMS(item : Item){

    nameLabel.text = item.name

    descriptionLabel.text = item.description

    priceLabel.text = convertCurrency(item.price )

    priceLabel.adjustsFontSizeToFitWidth = true // this will adjust the font of the label sizse to helpt it fit even with a large price



    if item.imageLinks != nil  && item.imageLinks.count > 0 {

        downLoadImages(imageUrls: [item.imageLinks.first!]) { (images) in
            self.ItemImageView.image = images.first as? UIImage
        }
    }
}

标签: swiftfirebase

解决方案


正如错误提示的那样,您需要从主线程更新 UI:

downLoadImages(imageUrls: [item.imageLinks.first!]) { (images) in
    DispatchQueue.main.async {
        self.ItemImageView.image = images.first as? UIImage
    }
}

推荐阅读