执行功能时,在导航控制器上单击后退按钮时,应用程序崩溃

时间:2019-01-20 23:43:20

标签: ios swift uiviewcontroller uinavigationcontroller

在慢速连接上,当我单击VC上的“后退”按钮时,访问导航控制器时会崩溃。 VC已经被释放,但是setNavBarTitle将在返回另一个视图后执行。我知道在VC已经释放的同时函数正在执行,但是我不确定处理这种情况的最佳方法是什么?

override func viewWillAppear(_ animated: Bool){
    super.viewWillAppear(animated)

    fetchProfile(clientId: clientId) { (result, error) in
        if result?.data != nil { 
                if (result?.success)! {
                    self.clientProfile = result!.data!
                    // Avatar
                    let clientImageView = UIImageView()

                    if let url = URL(string: result!.data!.pic_url!) {
                        clientImageView.image = UIImage(named: "System/defaultAvatar")
                        let task = URLSession.shared.dataTask(with: url) { data, response, error in
                            guard let data = data, error == nil else { return }

                            // WARNING: UIImageView.image must be used from main thread only                                
                            clientImageView.image = UIImage(data: data)
                            self.setNavBarTitle(image: clientImageView.image!)
                        }
                        task.resume()
                    } 
                }
            } 
        }
    }
}

private func setNavBarTitle(image: UIImage) {
    // Crashes here -> Thread 11: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
}

1 个答案:

答案 0 :(得分:0)

您可以尝试在方法回调中使用[weak self],因此,即使在VC释放之后调用该方法,也不会导致崩溃,因为self将不存在:

override func viewWillAppear(_ animated: Bool){
    super.viewWillAppear(animated)

    fetchProfile(clientId: clientId) { [weak self] (result, error) in
        if result?.data != nil { 
                if (result?.success)! {
                    self?.clientProfile = result!.data!
                    // Avatar
                    let clientImageView = UIImageView()

                    if let url = URL(string: result!.data!.pic_url!) {
                        clientImageView.image = UIImage(named: "System/defaultAvatar")
                        let task = URLSession.shared.dataTask(with: url) { data, response, error in
                            guard let data = data, error == nil else { return }

                            DispatchQueue.main.async {
                                clientImageView.image = UIImage(data: data)
                                self?.setNavBarTitle(image: clientImageView.image!)
                            } 


                        }
                        task.resume()
                    } 
                }
            } 
        }
    }
}

private func setNavBarTitle(image: UIImage) {
    // Crashes here -> Thread 11: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    guard let navigationBarHeight: CGFloat = self.navigationController?.navigationBar.frame.height else {
        return
    }
}