我想知道如何处理这个特定问题:
我正在创建一个地点对象,该对象使用地理经纬度对进行初始化。然后我使用 iOS 地理编码器来获取该坐标的地址。我想将该地址设置为我的实例变量之一。然而,这个异步调用没有及时完成,所以当我实例化我的对象并尝试显示地址时,它还没有完成。处理此问题和类似问题有哪些策略?
谢谢!圣诞快乐!
I'm wondering how to deal with this particular issue:
I'm creating a place object, which gets initialized with a geographical lat-long pair. Then I use the iOS geocoder to get an address for that coordinate. I want to set that address to one of my instance variables. However, this asynchronous call doesn't get completed in time, so when I instantiate my object and try to display the address, it hasn't been done yet. What are some strategies to deal with this and similar problems?
Thanks! Merry Christmas!
发布评论
评论(1)
我不想在平安夜创建一个广泛的答案,所以我现在会在这里给出一个简短的答案,如果您有问题和/或想要更多详细信息,请稍后进行编辑。
异步所有请求都有委托/协议方法,让您知道请求何时失败或成功。您应该使用 NSNotification API 并注册任何需要地址的对象,以在对象完成请求时触发通知。当对象收到通知时,它可以配置其视图或任何需要执行的操作。在 requestDidFinish (或其他)方法中,您应该发送通知。
查看这篇文章了解详细信息(以及有关线程的一些很酷的东西!):http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial
I don't feel like creating an extensive answer on Christmas Eve so I'll give a brief answer here for now, and edit it later if you've got questions and/or want more details.
Asynchronous requests all have delegate/protocol methods that let you know when the request has failed or succeeded. You should use the NSNotification API and register any object that needs the address for a notification that's triggered when the object completes the request. When the object receives the notification, it can then configure its views or whatever it needs to do. In the requestDidFinish (or whatever) method, you should send the notification.
Check out this article for details (as well as some cool stuff about threading!): http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial