iosswiftexceptionalamofirenserror

Error handling for cancelled request


I use Alamofire to send a download request. I am handling Success and Failure cases as shown below.

Alamofire.request {
     case Success:
          // Update UI
     case Failure:
          // Show Alert message (error!.localizedDescription)
}

Everything is perfectly fine.

When I go back, in viewWillDisappear, I cancel any ongoing request. The issue is, canceling the request throws the error message and that triggers the alert when I am not on that screen.

To my knowledge, I can take two actions.

  1. Check the condition if the error is created due to request cancel
  2. Check if the viewController is alive
  3. Also I can set a Bool variable in viewWillDisappear, which is a simple solution

How to handle the situation?

Also how to check point 1 & 2?


Solution

  • If you are able to get the reason why it has failed, you should go with that, so: Check the condition if the error is created due to request cancel.

    Option 2 is fragile, because you are relying on the UIViewController's life cycle. Option 3 will only make your logic more cumbersome, via a new flag. Keep it simple