如何等待包含API调用的函数的执行?

问题描述 投票:0回答:1
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.rentalServiceTypeBtn.isHidden=true
        self.outStationServiceTypeBtn.isHidden=true

        rideNowBtn.setTitle("RIDE NOW", for: .normal)
        var temp = Bool()
        temp = shouldCallRequestCheck()

        print("TEMP is \(temp)/n/n/n/n")
        if shouldCallRequestCheck()==false{
            self.BackgroundPerformStop()
            onRequestCheck()
        }
        else{

            BackgroundPerformStart()
        }

函数shouldCallRequestCheck()会调用API并根据响应设置布尔值并执行下一行代码。我想在执行下一行之前等待函数的响应。但是我的if else函数会在收到API的响应之前执行。我知道解决方案是将功能排队或使用同步。但我从未与他们合作过。

ios swift api asynchronous
1个回答
0
投票

然后完成它

func shouldCallRequestCheck(com:@escaping() ->()){
  Api.call {
     com()
  }
}

通话

shouldCallRequestCheck {
   // write your lines here  
}
© www.soinside.com 2019 - 2024. All rights reserved.