简体   繁体   English

异步功能不更新变量

[英]Asynchronous function not updating variable

        var endLat: Double = 0.0
        var endLong: Double = 0.0

              func forwardGeocodingStarting(address: String) {
            CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
                if error != nil {
                    print(error)
                    return
                }

                let placemark = placemarks?[0]
                let location = placemark?.location
                let coordinate = location?.coordinate

                dispatch_async(dispatch_get_main_queue()) {
                    startLat = (coordinate?.latitude)!
                    startLong = (coordinate?.longitude)!
                    print("\(startLat) is lat and \(startLong) is long")
                }
            })
        }

Hello, here is a geocoding function that I created, that simply takes an address and returns the address's coordinates. 您好,这是我创建的地理编码函数,该函数只获取一个地址并返回该地址的坐标。 My problem is, that at the end of the code section, when I do print(endLat) it prints out 0, however when I do it in the dispatch_async in the function, it comes out to the proper latitude. 我的问题是,在代码部分的末尾,当我执行print(endLat)它会打印出0,但是当我在函数的dispatch_async中执行时,它会显示适当的纬度。

I realize this is an asynchronous function, which is why I tried to use the dispatch_async to update the variable instantly. 我意识到这是一个异步函数,这就是为什么我尝试使用dispatch_async立即更新变量的原因。

My question is, how can I have these variables update instantly? 我的问题是,如何让这些变量立即更新? Thanks in advance. 提前致谢。

You cannot have these variables update instantaneously, as you are calling an asynchronous function. 在调用异步函数时,不能使这些变量立即更新。 Thats an obvious contradiction :-) 那是一个明显的矛盾:-)

That you dispatch back to the main queue is fine, but that doesn't change the asynchronous behaviour. 您分派回主队列是可以的,但这并不会改变异步行为。 In fact your are even issuing yet another dispatch_async ... (meaning the body of that code will get run at some later point in time) 实际上,您甚至还在发出另一个dispatch_async ...(这意味着该代码的主体将在以后的某个时间点运行)。

You could wait for the results to be available, eg using a dispatch group , but quite likely that is not what you want here. 您可以等待结果可用,例如使用调度组 ,但是很有可能这不是您想要的。 I assume you want to use the values in some view controller? 我假设您想在某些视图控制器中使用这些值? In this case you probably fill in some instance variables as the results arrive and update the UI state once both values are available. 在这种情况下,您可能会在结果到达时填写一些实例变量,并在两个值均可用时更新UI状态。

You've got a little confused with what you mean by "Asynchronous function not updating variable" . 您对"Asynchronous function not updating variable"含义感到有些困惑。 forwardGeocodingStarting(_:) is NOT an Async function. forwardGeocodingStarting(_:) 不是异步函数。 geocodeAddressString is an async function designed by the API to allow you to use the following values you get after the operation is done within that function only. geocodeAddressString是由API设计的异步函数,允许您仅使用在该函数中完成操作后使用的以下values Hence you keep getting 0 as the it doesn't wait till the operation is done completely to display the value. 因此,您一直保持为0,因为它不会等到操作完全完成以显示该值。 dispatch_async(dispatch_get_main_queue) is used only to update UI on the Main thread. dispatch_async(dispatch_get_main_queue)仅用于更新主线程上的UI。 You've to use a completion handler in your function like so: 您必须在函数中使用完成处理程序,如下所示:

 typealias theDouble = (Double, Double) -> ()  

 func forwardGeocodingStarting(address: String, completion: theDouble){
//your code
let aVar: Double!
let bVar: Double!

let placemark = placemarks?[0]
let location = placemark?.location
let coordinate = location?.coordinate
aVar = (coordinate?.latitude)!
bVar = (coordinate?.longitude)!
completion(aVar, bVar)
}

Now when you call your function, update the variables startLat and startLong like so: 现在,当您调用函数时,像这样更新变量startLatstartLong

forwardGeocodingStarting(<your string address>) { firstVar, secondVar in

startLat = firstVar
startLong = secondVar
 print("\(startLat) is lat and \(startLong) is long") //You will get correct values

}

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

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