0

I need to call func fillFields after that func getJsonData is over.

The func getJsonData is a Async Task for get data on server for URLRequest.

func getAPIData() {
    let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")

    DispatchQueue.main.async {
        _ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
            if requisicao != nil {}

            if let dados = dados {
                do {
                    let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]
                    /*
                    *
                    */                   
                } catch {
                    print(erro as Any)
                }
            }
        }.resume()
    }
}

How can I know if the function getAPIData is finished?

Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
Donadon
  • 113
  • 11
  • Do you tried my answer? – Nikunj Kumbhani Mar 04 '19 at 10:20
  • Hey @NikunjKumbhani, I will try and I post here – Donadon Mar 04 '19 at 13:40
  • Hey @NikunjKumbhani, This error appear: Main Thread Checker: UI API called on a background thread: -[WKWebView evaluateJavaScript:completionHandler:] – Donadon Mar 04 '19 at 14:10
  • Your solution seems to be the correct one for this case but it is giving error because the "fillFields" function that should be called after the URLRequest termination is triggering an error. As I'm using a WKWebView, fillFields calls a javascript function to refresh the screen – Donadon Mar 04 '19 at 14:23

1 Answers1

0

You can Identify with Completion handler when the task is complete like this.

func getAPIData(complition:@escaping (AnyObject?, Error?) -> Void) {

        let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")

        DispatchQueue.main.async {
            _ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
                if requisicao != nil {}

                if let dados = dados {
                    do {
                        let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]

                        complition(json as AnyObject, nil) // When Complete task

                        // Call next function Here

                    } catch {
                        print(erro as Any)
                        complition(nil, erro)
                    }
                } else {
                    complition(nil, erro)
                }
                }.resume()
        }
}

Call like this

self.getAPIData { (response,error) in
    print(response) // Your response is here after complete task
}
Nilesh R Patel
  • 697
  • 7
  • 17
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
  • complition? Plus, you are not escaping the closure when a task is complete. – El Tomato Mar 04 '19 at 05:19
  • So, as the next function is a screen update, it appears this error: Main Thread Checker: UI API called on a background thread: -[WKWebView evaluateJavaScript:completionHandler:] – Donadon Mar 04 '19 at 14:11