How to make a function call in ios to wait, till the block inside that function is executed completely?

心不动则不痛 提交于 2019-12-04 07:56:00

First, I would suggest to rethink your design. Instead of returning the zipCode value from this method, call into some other method in the completionHandler (create a protocol / delegate or whatever). The reverseGeocodeLocation:: method can take some time, and you don't want to pause execution of the main thread waiting on the result.

If you do want to block though, you might consider using (abusing?) a dispatch_semaphore_t. Initialize it to 0 and dispatch_semaphore_wait after the call to reverseGeocodeLocation::. In the completionHandler signal it with dispatch_semaphore_signal.

More information: Using Dispatch Semaphores to Regulate the Use of Finite Resources

edit: and like others suggested, declare zipCode with a __block qualifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!