问题描述
我需要实现某种数据发布到在后台的web服务器。只是为了澄清,通过在后台,我不是指呈现出旋转图标,并使用类似的数据发布到web服务的正常方式的AsyncTask
或 ASIHTT prequest
的 [请求startAsynchronous]
方法。我需要保持数据的队列线程可以异步地开始处理和张贴到一个Web服务,而用户在应用程序的工作。
I need to implement posting some data to a web server in the background. Just to clarify, by "in the background", I don't mean the normal way of showing a spinning icon and posting data to a web service using something like an AsyncTask
or ASIHTTPRequest
's [request startAsynchronous]
method. I need to maintain a queue of data that a Thread can asychronously start processing and posting to a Web service while the user is working in the application.
我在找上接收呼叫设计这样的队列,尤其是在一些边缘情况下,像用户一些帮助,退出应用程序,而职位正在发生,用户离开应用程序转到一个不同的,而后发生等等。你会如何处理这些情况?是否有任何来源$ C $ C可以推荐,这是否?
I'm looking for some help on designing a queue like that, especially in some edge cases like User receiving a call, logging out of the application while the the post is happening, user leaving the application to goto a different one while a post is happening and the like. How would you handle these cases? Is there any source code you can recommend that does this?
谢谢,结果
Teja公司。
Thanks,
Teja.
推荐答案
我在我自己的工作中使用 NSOperationQueue
近来,用于控制后台网络请求启动。 的NSOperation
涉及最样板code所必需的在后台异步上运行的线程的任务(如网络操作)(或前景,如果必要的UI更新) 。
I've started using NSOperationQueue
in my own work lately, for controlling background network requests. NSOperation
deals with most of the boilerplate code necessary for asynchronously running tasks (such as network operations) on threads in the background (or foreground, if necessary for UI updates).
它还允许在队列的依赖;例如,我用两个队列在我的应用程序:
It also allows dependencies across queues; for example, I use two queues in my application:
第一时间表下载图像,在一次为2的最大并发,在后台运行。每个图像下载都有相应的完成处理程序(作为 NSBlockOperation
),它是依赖于图像的下载完成的。这些操作坐在 [NSOperationQueue mainQueue]
,经营主线程上,使他们能够更新UI(具体而言,相应的的UIImageView
)。
The first schedules image downloads, at a max concurrency of 2 at a time, in the background. Each image download has a corresponding completion handler (as an NSBlockOperation
) that is dependent on the image download completing. These operations sit on the [NSOperationQueue mainQueue]
, which operates on the main thread, allowing them to update UI (specifically, the corresponding UIImageView
).
请注意该的NSOperation
和 NSOperationQueue
不是对网络请求只,但可以分为原子的任何操作任务和并行调度。
Note that NSOperation
and NSOperationQueue
are not for network requests only, but any operation that can be divided into atomic tasks and scheduled concurrently.
这里是苹果对专题介绍文档。
Here are Apple's intro docs on the topic.
这篇关于背景网络调用 - iOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!