2017-03-14 1 views
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, 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() 
    } 
+0

하는 당신은 UINavigationController를 사용하고 있습니까 :

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

당신은 당신이 사용하는 것이 모달 SEGUE을 사용하는 경우? –

+0

UINavigationController를 사용하고 있습니다 – SwiftyJD

답변

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() 
    } 

보기 컨트롤러에서 당신은 내가 이런 식으로 그것을 사용하는 것이이 메서드를 호출 : 여기에 코드입니다.

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) 

} 
+0

이 요청은 싱글 톤 내에서 이루어 지지만 viewcontrollers가 액세스합니다 – SwiftyJD

+0

내 대답을 편집했습니다. 요청이 성공하면 두 개의 응답이 추가되고 실패하면 두 개의 응답이 추가되었습니다. 케이스에 따라 둘 다 관리 할 수 ​​있습니다. – unniverzal

+0

생각 나는 대답을 좋은 것으로 표시했다. – SwiftyJD

0

시도해 볼 수 있습니다. 나를

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

이이 UINavigationController 스택에서 최상위 UIViewController을 나타납니다이 결과로 이전 화면이 표시됩니다, 노력하고 있습니다. 당신이 당신의 현재 VC에 도착 쇼 SEGUE을 사용한 경우

0

당신은 사용할 수 있습니다

_ = self.navigationController?.dismiss(animated: true) 
관련 문제