ios - nsoperationqueue - 从服务器下载图像
我想在我的应用程序中使用 NSOperationQueue 并开始从服务器下载图像。 如果我的应用程序进入后台或被终止,NSOperationQueue 是否仍会继续下载它们?
I want to use NSOperationQueue in my app and start downloading the images from server.
If my app goes to background or be terminated, will NSOperationQueue still continues downloading them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,当应用程序移至后台时,
NSOperationQueue
将不会继续工作。您需要使用beginBackgroundTaskWithExpirationHandler
方法显式执行此操作。技术说明 TN2277 - 网络和多任务对此进行了介绍。
请参阅还有iOS 应用程序编程指南,特别是该部分关于应用程序状态和多任务处理。
顺便说一句,我可以建议您改用 AFNetworking 库。它为您处理很多此类功能。具体来说,每个类都是 NSOperation 的子类。
此外,它已经在 AFImageRequestOperation 中有一个图像下载器类。所以这对你来说应该非常有用。
AFImageRequestOperation
是 AFURLConnectionOperation 的子类因此您可以访问方法setShouldExecuteAsBackgroundTaskWithExpirationHandler
。当然,所有这些都与多任务处理有关,因此它仅在 iOS 4.0 及更高版本中可用。
No, the
NSOperationQueue
will not continue working when the app is moved to the background. You would need to explicitly action this by using the methodbeginBackgroundTaskWithExpirationHandler
.This is covered in Technical Note TN2277 - Networking and Multitasking
See also iOS App Programming Guide, specifically the section on App States and Multitasking.
As an aside, could I recommend that you instead use the AFNetworking library. It handles a lot of this functionality for you. Specifically, each class is a subclass of
NSOperation
.Moreover, it already has an image downloader class in AFImageRequestOperation. So that should be very useful for you.
AFImageRequestOperation
is a subclass of AFURLConnectionOperation so you have access to the methodsetShouldExecuteAsBackgroundTaskWithExpirationHandler
.Of course, all this relates to multitasking so it is only available in iOS 4.0 and later.