title: swift 4 error : '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread
body: <p>The app works well when the internet connection is active. However, I tried switching off the internet connection and I tried using the end point. </p>
  
  <p>I got this error:</p>
  
  <blockquote>
    <p>*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'</p>
  </blockquote>
  
  <p>A few points:</p>
  
  <p>a) First off, I am not clear about which place I should use the async - I have put it in two places for two different cases in a switch statement.</p>
  
  <p>b) Second, am I handling the error well with the <code>error.localizedDescription</code>? What I wanted to do was to find a way to handle the 1009 error when the internet is off.</p>
  
  <p>If I have asked for unnecessarily long answer, please guide me to a resource where I can read about it.</p>
  
  <pre><code> let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
              if let error = error{
                  self.errorMessage += "Data Gathering Error: " + error.localizedDescription + "\n"
                  completion(self.errorMessage, nil)
                  return
  
          } else if let data = data, let response = response as? HTTPURLResponse{
              print(response.statusCode)
  
              if response.statusCode == 200 {
                  do {
                      switch relativeURL{
                      case .requestOTP:
                          print("------------")
                          print(String(data: data, encoding: .utf8)!)
                          print("------------")
  
                          let responseInfo = try JSONDecoder().decode(loginResponse.self, from: data)
                          print(responseInfo.success)
                          print(responseInfo.message)
                        DispatchQueue.main.async {
                          let dataReceived = responseData(loginResponse: .init(success: responseInfo.success, error: .init(message:responseInfo.error?.message), message: responseInfo.message), decodeOTPResponse: nil,quotaResponse:nil,UserAddResponse:nil)
  
                          print(dataReceived)
  
                                      completion(nil,dataReceived)
                          }
  
  
  
                      case .loginWithOTP:
                          let responseInfo = try JSONDecoder().decode(decodeOTPResponse.self, from: data)
  
  
                          let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: .init(success: responseInfo.success, token: responseInfo.token, error: .init(message:responseInfo.error?.message), totp_secret: responseInfo.totp_secret),quotaResponse:nil,UserAddResponse:nil)
                          print(dataReceived)
  
                          DispatchQueue.main.async {
                              completion(nil,dataReceived)
  
                          }
                      case .addUser:
                          let responseInfo = try JSONDecoder().decode(UserAddResponse.self, from: data)
                          print(responseInfo)
  
                          let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: nil, quotaResponse: nil, UserAddResponse:.init(success: responseInfo.success, error:.init(message:responseInfo.error?.message), message: responseInfo.message))
                          DispatchQueue.main.async {
                              completion(nil,dataReceived)
                          }
  
  
                      default:
                          completion("Wrong request call",nil)
                          return
                      }
  
  
                  } catch let jsError{
                      print("Error serialising JSON", jsError)
                      completion("Error Serialising JSON",nil)
                      return
  
                  }
              } else if response.statusCode &gt; 401 &amp;&amp; response.statusCode &lt; 500{
  
                  print("Unauthorized to perform action")
  
  
              } else if response.statusCode == 500{
  
                  print("endpoint not found")
  
              } 
  
          }
  
      }
      task.resume()
  </code></pre>