0

I'm trying to set up the network session creator to go back to the previous screen if the timeout request runs out. As of now I'm not completely sure where or how to execute it. Here is the code:

  lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default

  lazy var session: URLSession = URLSession(configuration: self.configuration)

 typealias JSONData = ((Data) -> Void)

  func getJSONData(type: String, urlExtension: String, completion: @escaping JSONData) {

    configuration.timeoutIntervalForRequest = 5
    configuration.timeoutIntervalForResource = 5

    let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )

    let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in

      if error == nil {
        if let httpResponse = response as? HTTPURLResponse {
          switch (httpResponse.statusCode) {
          case 200:
            if let data = data {
              completion(data)
            }
          default:
            print(httpResponse.statusCode)
          }
        }
      } else {
        print("Error: \(error?.localizedDescription)")
      }
    })
    dataTask.resume()
  }
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

3 Answers3

0
 lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default

  lazy var session: URLSession = URLSession(configuration: self.configuration)

 typealias JSONData = ((Data) -> Void)

  func getJSONData(type: String, urlExtension: String, onSucceed : @escaping JSONData ,  onFailure: @escaping (_ error:NSError)-> Void ) {

    configuration.timeoutIntervalForRequest = 5
    configuration.timeoutIntervalForResource = 5

    let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )

    let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in

      if error == nil {
        if let httpResponse = response as? HTTPURLResponse {
          switch (httpResponse.statusCode) {
          case 200:
            if let data = data {
              onSucceed(data)
            }
          default:
            print(httpResponse.statusCode)
          }
        }
      } else {
        onFailure(error! as NSError)
      }
    })
    dataTask.resume()
  }

In the View Controller where you call this method I would use it like this.

NetworkManager.getJSONData(type: "", urlExtension: "",onSucceed {
   //doSmth
}, on Failure{(error) in 
   //show Error
// if it is pushed
    _ = self.navigationController?.popViewController(animated: true) 

// or if its presented 
// self.navigationController?.dismiss(animated: true, completion: nil)

}
unniverzal
  • 803
  • 10
  • 17
  • This request is made inside a singleton but is accessed by the viewcontrollers – SwiftyJD Mar 14 '17 at 14:09
  • I edited my answer , I added 2 closures : one if the request succeeds and the other one if it fails. Depending on the case you can manage them both. – unniverzal Mar 14 '17 at 14:25
0

You can try this. this is working for me

_ = self.navigationController?.popViewController(animated: true)

this will pop the top most UIViewController from the UINavigationController stack, as a result your previous screen will be visible.

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
0

If you used a show segue to get to your current VC you can use:

_ = self.navigationController?.popViewController(animated: true)

If you used a modal segue you would use:

_ = self.navigationController?.dismiss(animated: true)
Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49