Objective-C 协程
Objective-C - 我如何才能创建 Objective-C 协程?当我通过按钮调用 IBAction 时,它会冻结应用程序,直到 IBAction 完成检索数据(从网站)。我如何让它作为主应用程序的协程运行?
谢谢, 克里斯蒂安·斯图尔特
Objective-C - how might I be able to create a objective-c coroutine? When I have a IBAction called by a button it freezes the app until the IBAction finishes retreiving data (from a web site). How might I have this run as a coroutine to the main app?
Thanks,
Christian Stewart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
协程是一种具体的实现技术。您想要做的是在后台工作而不阻塞您的用户界面。
在 iOS 和 Mac OS X 上执行此操作有两种主要技术:运行循环和线程。
如果您只是通过 HTTP URL 下载一些数据,您可以开始下载并告诉它在完成或有一些数据时通知您。 URL 下载将使用当前运行循环管理其自身的并发性,并且您的应用程序将能够与用户交互。
如果您需要执行自己的处理,您可以使用 NSOperationQueue 和 NSOperatipn、使用 GCD (dispatch_*) 或直接使用 NSThread 来分出一个后台线程来执行此操作。如果您这样做,您需要了解并发编程、使用互斥体(锁)和其他同步机制等保护共享状态。
对于仅 URL 下载,请使用 NSURLDownload 自己的并发支持。对于更多处理繁重的工作,请尝试 NSOperation。
A coroutine is a specific implementation technique. What you want to do is do work in the background without blocking your user interface.
There are two primary techniques for doing this on iOS and Mac OS X: Run loops and threads.
If you are just downloading some data via an HTTP URL, you can start the download and tell it to notify you when done or when it has some data for you. The URL download will manage its own concurrency using the current run loop, and your application will be able to interact with the user.
If you need to perform your own processing, you can spin off a background thread to do it using NSOperationQueue and NSOperatipn, using GCD (dispatch_*), or using NSThread directly. If you do this you need to understand concurrent programming, protecting shared state using mutexes (locks) and other synchronization mechanisms, and so on.
For just a URL download, use NSURLDownload's own concurrency support. For more processing-heavy work, give NSOperation a try.
看看 URL加载系统编程指南。
您可能对NSURLConnection,您设置 URL 并提供委托。当连接/下载完成时,您的代表将收到数据呼叫。
Have a look at URL Loading System Programming Guide.
You are probably interested in NSURLConnection, you setup the URL and provide delegates. Your delegates get called with data and when the connection/download is finished.