I am a very BEGINNER in swiftui programming, so my excuses for any fool question.
I have a POST (URLSession) that is updating my variables only in the second run (I will explain)
I put a print to see where code is passing and in immediate panel shows like this:
Passed 01 ===========
Passed 03 ===========
Passed 04 =========== out off func!!
2021-05-04 11:41:16.460925-0300 Fuel[2348:100030] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
Passou 02 ===========
Status: true
Nome: Roberto Pires
(I didn't understand libquic failed, but the problem is another one)
Well, the correct should be:
Passed 01 ===========
Passou 02 ===========. <- SHOULD PASS HERE FIRST
Status: true
Nome: Roberto Pires
Passed 03 ===========
Passed 04 =========== out off func!!
2021-05-04 11:41:16.460925-0300 Fuel[2348:100030] [] nw_protocol_get_quic_image_blockinvoke dlopen libquic failed
My code is called by a button, but returns BEFORE complete (and as so, show an incorrect Alert, because the "myJsonResponse?.status"_ is false at this time), but the code is still running inside de func (surprisingly) and complete all the POST information correctly.
Why is not entering inside URLSession.shared.dataTask lines in the first time ?
Here is my code:
Code Block func validatingLogin() { guard let myUrl = URL(string: "http://www.rcpires.com/apps/main.asp") else { print("URL invalid") return } var request = URLRequest(url: myUrl) request.httpMethod = "POST" let postString = usuario.loginPost() request.httpBody = postString.data(using: String.Encoding.utf8) print(“Passed 01 ===========") URLSession.shared.dataTask(with: request) { data, response, error in print("Passed 02 ===========") guard let myJson = data else { print(“Data not found") return } if let decodedData = try? JSONDecoder().decode(MyJSONResponse.self, from: myJson) { DispatchQueue.main.async { self.myJsonResponse = decodedData print("Status: \(myJsonResponse?.status ?? false)") print("Nome: \(myJsonResponse?.nome ?? "")") return } } else { let dataString = String(decoding: myJson, as: UTF8.self) print (“Invalid answer: \(dataString.description)") return } }.resume() print("Passed 03 ===========") }
This is called by my Login Button:
Code Block Button(action: { validatingLogin() if aspStatus.success == false { print("Passed 04 ========== out off func!”) isAlert = true } else { isLogin = true } }, label: { HStack { Image("checkmark").frame(width: 25, height: 15, alignment: .leading) Text("Entrar").bold() } })
Thanks in advance to every good soul that helps me.
Seems your code is working correctly.I put a print to see where code is passing and in immediate panel shows like this:
Passed 01 ===========
Passed 03 ===========
Passed 04 =========== out off func!!
2021-05-04 11:41:16.460925-0300 Fuel[2348:100030] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
Passou 02 ===========
Status: true
Nome: Roberto Pires
(I didn't understand libquic failed, but the problem is another one)
NO, it is not correct.Well, the correct should be:
It is the right behavior, when you want to work with async methods. An async method itself returns to caller BEFORE complete, and the completion handler is executed after the task is completed.My code is called by a button, but returns BEFORE complete (and as so, show an incorrect Alert, because the "myJsonResponse?.status"_ is false at this time),
What is aspStatus? It does not appear in validatingLogin().This is called by my Login Button:
In the near future, you may need to learn async/await pattern in Swift, but you need to be accustomed with completion handler pattern until then.
You need to write everything you want to run after the task is completed within the completion handler.