简体   繁体   English

Swift异步调用并返回

[英]Swift async call and return

func sendPalletCheckScanAsUrl(viewController: UIViewController?, palletNr: String) -> PalletCheckHelper.PalletCheckerResponse  {
            var palletCheckerResponse = PalletCheckHelper.PalletCheckerResponse()
            var urlComponents = URLComponents()
            urlComponents = JsonHelper.defineUrlComponents() //this components are common for all requests
            urlComponents.path = "/Inventory/GetPalletDetail/OP"

            do {

                urlComponents.queryItems = [
                    URLQueryItem(name: "id", value: palletNr)
                ]
            } catch {
                //TODO:error handling
            }

            guard let url = urlComponents.url else { fatalError("Could not create URL from components") }

            // Specify this request as being a POST method
            var req = URLRequest(url: url)
            req.httpMethod = "GET"

            // Make sure that we include headers specifying that our request's HTTP body; this is done by helper function
            req = JsonHelper.defineCommonHeaders(request: req)
            print (req)


            JsonHelper.configSessionAndSendPalletNr(request: req) {
                returned in
                palletCheckerResponse = returned
                print ("returned JSON INSIDE", palletCheckerResponse)
            }

            print ("returned JSON OUTSIDE", palletCheckerResponse)

            return palletCheckerResponse
          }
    }

Something wrong here with async calls. 异步调用在这里出了点问题。 calling 呼唤

JsonHelper.configSessionAndSendPalletNr(request: req) gives me proper JSON sttring (print JSON INSIDE and all singing and dancing). JsonHelper.configSessionAndSendPalletNr(request: req)给了我适当的JSON sttring(打印JSON INSIDE以及所有唱歌和跳舞)。 Assigning returned to variable and printing outside is empty. 分配返回给变量并在外部打印为空。

returned JSON OUTSIDE PalletCheckerResponse(deliveryId: nil, bookedDate: nil, onSiteDateTime: nil, companyName: nil) 返回的JSON外部PalletCheckerResponse(deliveryId:nil,bookedDate:nil,onSiteDateTime:nil,companyName:nil)

returned JSON INSIDE PalletCheckerResponse(deliveryId: Optional("D113789"), bookedDate: Optional("2019-06-26T09:00:00"), onSiteDateTime: Optional("2019-06-26T10:33:23"), companyName: Optional("Omega")) 返回的JSON INSIDE PalletCheckerResponse(deliveryId:Optional(“ D113789”),bookedDate:Optional(“ 2019-06-26T09:00:00”),onSiteDateTime:Optional(“ 2019-06-26T10:33:23”),companyName:可选(“ Omega”))

What am I missing? 我想念什么?

This is how your method should look like 这就是您的方法的外观

func sendPalletCheckScanAsUrl(viewController: UIViewController?, palletNr: String, completionHandler : @escaping (PalletCheckHelper.PalletCheckerResponse) -> Void) {

    var palletCheckerResponse = PalletCheckHelper.PalletCheckerResponse()
    var urlComponents = URLComponents()
    urlComponents = JsonHelper.defineUrlComponents() //this components are common for all requests
    urlComponents.path = "/Inventory/GetPalletDetail/OP"

    do {

        urlComponents.queryItems = [
            URLQueryItem(name: "id", value: palletNr)
        ]
    } catch {
        //TODO:error handling
    }

    guard let url = urlComponents.url else { fatalError("Could not create URL from components") }

    // Specify this request as being a POST method
    var req = URLRequest(url: url)
    req.httpMethod = "GET"

    // Make sure that we include headers specifying that our request's HTTP body; this is done by helper function
    req = JsonHelper.defineCommonHeaders(request: req)
    print (req)

    JsonHelper.configSessionAndSendPalletNr(request: req) {
        returned in
        completionHandler(returned)
        print ("returned JSON INSIDE", palletCheckerResponse)
    }
    //        Remove  this line -> return palletCheckerResponse
}

This is because, by the time you receive your response in your configSessionAndSendPalletNr , the function returns the control. 这是因为,当您在configSessionAndSendPalletNr中收到响应时,该函数将返回控件。

Adding a closure will help. 添加闭包将有所帮助。

To know more read about following topics 要了解更多信息,请阅读以下主题

Closures in swift 快速关闭

@escaping vs @non-escaping @转义vs @非转义

Async calls in swift. 异步调用迅速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM