如何快速同步运行异步线程

时间:2019-01-22 19:30:03

标签: swift

代码

  func callcab(tag:Int){

    let makebook = PFObject(className:"bookingstwo")
    var droppoint=PFGeoPoint(latitude: dropmarker.position.latitude, longitude: dropmarker.position.longitude)
    makebook["Drop"]=droppoint
    makebook["Custid"]=getcustomerId()

    makebook["price"]=priceCalculation(tag: tag)

    makebook.saveInBackground {
        (success: Bool, error: Error?) in
        if (success) {


            print("makebook.objectId")


        } else {

        }
    }

}


     func getcustomerId()->String?
     {
    var results:String?
    let number=UserDefaults.standard.object(forKey: "phone") as? String
    let Stringtonumber=Int(number!)
    let s=PFQuery(className: "Customer")
    s.whereKey("phonenumber", equalTo:Stringtonumber!)
    s.getFirstObjectInBackground(block: { (gameScore: PFObject?, error: 
                          Error?) in
        if let error = error {
            //The query returned an error
            print(error.localizedDescription)
        } else {
            //The object has been retrieved
            print("aiyya jolly\(gameScore?.objectId)")
             results=gameScore!.objectId! as String

        }
    })




   return results
}

当我运行该程序时,我出错了(getcustomerId为零),我知道那是因为getFirstObjectInBackground方法是异步的,但是如何摆脱这个问题,因为iam经常倾向于处理这种异步方法,是否有任何合适的解决方案解决这个问题还是缺少什​​么?

0 个答案:

没有答案