Post

Replies

Boosts

Views

Activity

Reply to how can i make my async func wait
@IBAction func ValidButton(_ sender: Any) {     Task { @MainActor in       if verif() { //do some stuff }print("it pass throught") } verif() -> Bool { bool = true //do some stuff and make my bool false    var lat1 = 0.00       var lat2 = 0.00       var long1 = 0.00       var long2 = 0.00             Task {         do {           let coordinates = try await getLatLongFromAddress(withAddress: address1)           lat1 = coordinates.latitude           long1 = coordinates.longitude           print("when i call it : lat1 : ",lat1," long1 : ",long1)         } catch {           print(error)         }                   do {           let coordinates = try await getLatLongFromAddress(withAddress: address2)           lat2 = coordinates.latitude           long2 = coordinates.longitude           print("when i call it : lat1 : ",lat1," long1 : ",long1)         } catch {           print(error)         }       }       print(" lat1 : ", lat1," long1 : ",long1)       print(" lat2 : ",lat2," long2 : ",long2) //do some stuff return bool } //myfunction func getLatLongFromAddress(withAddress address: String) async throws -> CLLocationCoordinate2D {  let geocoder = CLGeocoder()       let placemarks = try await geocoder.geocodeAddressString(address)           let location = placemarks[0].location // for now we’ll assume [0] always exists       let result = location!.coordinate          return result // for now we’ll assume the coordinate always exists } here is the log :  lat1 : 0.0 long1 : 0.0  lat2 : 0.0 long2 : 0.0 2023-01-02 16:32:36.503786+0100 myApp[8758:254961] [Client] {"msg":"#NullIsland Received a latitude or longitude from getLocationForBundleID that was exactly zero", "latIsZero":0, "lonIsZero":0, "location":'80 B6 C8 08 03 00 00 00'} end it pass through when i call it : lat1 : 43.5982309 long1 : 1.4313821 2023-01-02 16:32:36.554774+0100 myApp[8758:254659] [Client] {"msg":"#NullIsland Received a latitude or longitude from getLocationForBundleID that was exactly zero", "latIsZero":0, "lonIsZero":0, "location":'80 36 D1 08 03 00 00 00'} when i call it : lat1 : 43.5982309 long1 : 1.4313821 default
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to how can i make my async func wait
@IBAction func validButton(_ sender: Any) {     Task { @MainActor in       if verif() {   let date = universalDate         let address = self.addressEditTextClient.text         let cp = self.cpEditTextClient.text         let city = self.villeEditTextClient.text         let accessibilite = self.villeEditTextClient.text         let type_chantier = self.typeChantierEditTextClient.text         let email = email                   var urlLink = "my link"                   //dateFormat                   let parameters = [my parameter]               var sParams = ""                   for (key,value) in parameters{           //domain.com?parameter1Key=parameter1Value&parameter2Key=parameter2Value           sParams += key + "=" + (value) + "&"           print("\(key),\(value)")                     }         if !sParams.isEmpty{           sParams = "?" + sParams                       if sParams.hasSuffix("&"){             sParams.removeLast()                         }                       urlLink = urlLink + sParams         }else{           print("!sParams.isEmpty fail")         }                   let serializer = DataResponseSerializer(emptyResponseCodes: [200,204,205])                   let urlString = urlLink.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! //This will fill the spaces with the %20                   //API         let request = URLRequest(url : URL(string:urlString)!)         AF.request(request).uploadProgress{progress in }.response(responseSerializer: serializer){ [self] response in           if response.error == nil {             var responseString: String!                           responseString = ""                           if response.data != nil {               //receive data-->responseString and put into dataJson               responseString = String(bytes:response.data!, encoding: .utf8)               var dataJson = responseString               //remove [ ] on the dataJson               dataJson = dataJson!.replacingOccurrences(of: "[", with: "")               dataJson = dataJson!.replacingOccurrences(of: "]", with: "")                               let jsonObjectData = dataJson!.data(using: .utf8)!               let data = try? JSONDecoder().decode(                 Data.self,                 from: jsonObjectData               )               print(data?.result as Any)             }else{               responseString = response.response?.description             }             print("response time: \(response.metrics?.taskInterval.duration ?? 0)")           }         }       }       else{         print("it pass through")       }     }   } here is verif :   func verif() -> Bool {     var check = true;     textBlanc()           var message = "Attention : \n";           if !checkIssetEditText() {       message += " - il faut remplir tous les champs pour valider";       check = false;     } else {       let address1 = makeAddress(let: self.addressEditTextClient.text!,                     let: self.cpEditTextClient.text!,                     let: self.villeEditTextClient.text!)       let address2 = makeAddress(let: "Toulouse",                     let: "",                     let: "")       var lat1 = 0.00       var lat2 = 0.00       var long1 = 0.00       var long2 = 0.00                     Task {         do {           let coordinates = try await getLatLongFromAddress(withAddress: address1)           lat1 = coordinates.latitude           long1 = coordinates.longitude           print("when i call it : lat1 : ",lat1," long1 : ",long1)         } catch {           print(error)         }                   do {           let coordinates = try await getLatLongFromAddress(withAddress: address2)           lat2 = coordinates.latitude           long2 = coordinates.longitude           print("when i call it : lat1 : ",lat1," long1 : ",long1)         } catch {           print(error)         }       }                          print(" lat1 : ", lat1," long1 : ",long1)       print(" lat2 : ",lat2," long2 : ",long2)               if !checkCP(let: self.cpEditTextClient.text!){                   cpEditTextClient.textColor = .yellow                   message += " - le code postal est incorrect \n"         check = false       } else if !checkAddress(let: lat1, let: lat2, let: long1, let: long2, let : 55.0){                   addressEditTextClient.textColor = .yellow         cpEditTextClient.textColor = .yellow         villeEditTextClient.textColor = .yellow                   message += " - les livraisons ne peuvent aller au-delà de 50 km de la ville \n"         check = false       }     }           if !check {       let alert = messageAlerte(let: message,let:"")       self.present(alert, animated: true, completion: nil)     }     print("end")     return check         } and my function and log is still the same i just remove my url and my parameter from my button
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to how can i make my async func wait
i try it with another way to do it because my task still didn't work so how i did it is :   getLatLongFromAddress(withAddress: address1) { (lat,long) in         lat1 = lat         long1 = long         print("when i call it : lat1 : ",lat1," long1 : ",long1)            getLatLongFromAddress(withAddress: address2) { (lat,long) in         lat2 = lat         long2 = long         print("when i call it : lat2 : ",lat2," long2 : ",long2)           //do my stuff       }       } i totally change my whole code it become ugly but atleast it work im really thanksfull to you @Scott and @Claude31 i learn a lot about Async i didn't know about that
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23