我目前在我的 iOS 应用程序中遇到线程问题。经过测试,问题似乎与WebKit集成有关。
这里是
showTrailerViewController
类的相关部分:
class showTrailerViewController: UIViewController {
private let webView: WKWebView = {
let webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
override func viewDidLoad() {
view.addSubview(webView)
}
func configure(with model: ShowTrailerViewModel) {
// Tasks that the user has initiated and requires immediate results
DispatchQueue.global(qos: .userInitiated).async {
// Perform network request on background thread
guard let url = URL(string: "\(model.youtubeView.id.videoId)") else { return }
let urlRequest = URLRequest(url: url)
DispatchQueue.main.async {
// Update UI on main thread
self.showLabel.text = model.show
self.overviewLabel.text = model.showOverview
self.webView.load(urlRequest)
}
}
}
}
这里是配置和运行
showTrailerViewController
的协议及其实现:
protocol CollectionViewTableViewCellDelegate: AnyObject {
func CollectionViewTableViewCellDidTapCell(_ cell: CollectionTableViewCell , viewModel: ShowTrailerViewModel)
}
extension HomeViewController: CollectionViewTableViewCellDelegate {
func CollectionViewTableViewCellDidTapCell(_ cell: CollectionTableViewCell, viewModel: ShowTrailerViewModel) {
let vc = showTrailerViewController()
vc.configure(with: viewModel)
if self.navigationController?.viewControllers.contains(vc) == false {
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
有人能帮我理解为什么 WebKit 集成会导致线程出现问题以及如何解决吗?谢谢。