异步调用(特别是 iOS 地理编码)

发布于 2024-12-22 16:48:41 字数 179 浏览 3 评论 0 原文

我想知道如何处理这个特定问题:

我正在创建一个地点对象,该对象使用地理经纬度对进行初始化。然后我使用 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-12-29 16:48:41

我不想在平安夜创建一个广泛的答案,所以我现在会在这里给出一个简短的答案,如果您有问题和/或想要更多详细信息,请稍后进行编辑。

异步所有请求都有委托/协议方法,让您知道请求何时失败或成功。您应该使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文