The internet connection appears to be offline intermittently

I have multiple request that I call on the viewDidLoad method of my initial view controller. Sometimes these requests are failing with `Error Code -1009 The internet connection appears to be offline.` for some user and others are not having it. Sometimes users who get the error can use it after sometime with no issues. This usually happens on a cellular network. And other apps are working in their phone and not ours.


All of these requests are using the same method on my Service class. First I have an enum of the APIs and I convert them to a URLRequest using Alamofire's URLRequestConvertible protocol. Then I pass this request into Alamofire and handle the response.



func get(_ api: MoonshineAPI, resultType: T.Type, completion: @escaping (_ result: T?, _ error: Error?) -> ()) {
        Alamofire.request(api).validate().responseJSON { response in
          switch response.result {
          case .success(let json):
            print("\(api.path):\n\(json)")
            do {
              if let data = response.data {
                let jsonDecoder = JSONDecoder()
                let dictionary = try jsonDecoder.decode([String: T].self, from: data)
                if let result = dictionary["result"] {
                  completion(result, nil)
                } else {
                  completion(nil, self.resultNotFoundError)
                }
              }
            } catch {
              completion(nil, error)
            }
          case .failure(let error):
            error.trackMixPanelEvent()
            completion(nil, error)
          }
        }
      }


Since I am creating an instance of the Service class and calling the get method on it for each request, is it possible that the request is being deallocated? What could also be the cause of such intermittent network error?

Since I am creating an instance of the Service class and calling the get method on it for each request, is it possible that the request is being deallocated?

As you’re not using

URLSession
directly, I can’t offer any insight into that. I recommend you ask that question via the support channel for the library you’re using.

What could also be the cause of such intermittent network error?

The most likely cause of intermittent network errors is… well… intermittent network errors, that is, the network itself dropping in and out. It is, however, hard to prove that. There are two standard tools for investigating problems like this:

However, it’s hard to get this information from folks in the field. Ideally you’d like to be able to reproduce the problem yourself. Failing that, you should see if you can engage with a technically savvy user who can reproduce the problem reasonably reliably and is able to run diagnostic tests on your behalf.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
The internet connection appears to be offline intermittently
 
 
Q