0

I have question regarding this piece of code :

func sendDataToBackend()  {

    Alamofire.request(MoneyCupUsersBackEndRouter.sendBI(BudgetInsightConnectionData(budgetInsightResponse: (self.BudgetInsightJSON)!, budgetInsightPermanentToken: (self.permanentToken)!, srcDate: userData!.lastUpdatedAt))).validate().responseString
        { [weak self] response in

            switch response.result{
            case .success( _):
                DispatchQueue.main.async {
                    SVProgressHUD.dismiss()
                    _ = self?.navigationController?.popToRootViewController(animated: true)

                }

            case .failure(let error):
                self?.showError(title: "ERROR SENT DATA BACKEND", message: "Erreur lors de l'envoi des données au Backend", error: error)

            }
    }


}

func showError(title: String, message: String, error: Error) {
    print(title)
    print(error)
    DispatchQueue.main.async {
        SVProgressHUD.dismiss()
        let alert = UIAlertController(title: "erreur", message: message, preferredStyle:.actionSheet)
        alert.addAction(UIAlertAction(title: "OK", style: .default)
        { Void in
            _ = self.navigationController?.popToRootViewController(animated: true)}
        )
        self.present(alert, animated: true, completion: nil)
    }


}

The function showError is called in a closure. But the function also deals with the self object. Since the showError is called within the closure, am I creating a strong reference to self with the call ? If so, I do I get around the problem ?

user3239711
  • 639
  • 1
  • 7
  • 24

1 Answers1

1

There’s no problem in your code because showError is captured weak and the DispatchQueue.main.async closure doesn’t cause a retain cycle.

vadian
  • 274,689
  • 30
  • 353
  • 361