AFNetworking可以处理请求队列吗? [英] Can AFNetworking handle a queue of requests?
问题描述
我在iOS 6上的示例:
My example on iOS 6:
- 10需要将多部分请求(按顺序)发送到服务器。
(因此请求形成一个队列) - 应显示进度。
- 如果一个请求失败,则所有后续操作都将失败
- 请求队列应该可以取消
- 10 Multi-Part requests need to be sent (in order) to the server. (so the request forms a queue)
- progress should be shown.
- if one request fails all following should fail
- a request queue should be cancellable
AFNetworking可以帮我解决这个问题吗?或者我应该尝试用NSOperations构建一些东西并自己运行循环?
Can AFNetworking help me with this? Or should I try to build something with NSOperations and run the loops myself?
如果我需要在这些请求之间传递上下文数据,例如第一个请求产生的事务id 。是否有任何关于我需要考虑的线程可见性的考虑因素?
If I need to pass context data between theses requests for example a transaction id produced by the first request. Are there any considerations about thread visibility I need to consider?
推荐答案
AFNetworking可以做到这一点。我建议您使用AFHTTPRequestOperationManager(它本身使用NSOperation),而不是AFHTTPSessionManager。有很多方法可以使用AFHTTPSessionManager来实现它,但没有一种方法可以像操作一样优雅。
AFNetworking can do this. I recommend that you use AFHTTPRequestOperationManager (which itself uses NSOperation), rather than AFHTTPSessionManager. There are ways to do it with AFHTTPSessionManager, but none as elegant as with operations.
在幕后,这是你没有经理的情况:
Under the hood, here's what you'd do without the manager:
您将使用请求序列化程序来创建NSMutableURLRequest(例如, [AFHTTPRequestSerializer -multipartFormRequestWithMethod:URLString:parameters:constructBodyWithBlock:error:]
;也有类似的JSON请求序列化程序)。
You will use a request serializer to make your NSMutableURLRequest (for example, [AFHTTPRequestSerializer -multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:]
; there's a similar JSON request serializer too).
获得URL请求后,使用 [AFHTTPRequestOperation -initWithRequest:]
进行操作。您还应设置其完成块。
Once you have a URL Request, make the operation with [AFHTTPRequestOperation -initWithRequest:]
. You should also set its completion blocks.
最后,将您的操作添加到 [AFHTTPRequestOperationManager manager] .operationQueue
和开始
它。
Finally, add your operation to [AFHTTPRequestOperationManager manager].operationQueue
and start
it.
现在您已了解这基本上是如何一起工作,这是一个更简单的方法:
Now that you understand how this is basically all working together, here's a simpler approach:
- 子类
AFHTTPRequestOperationManager
,可选择设置requestSerializer
如果您不喜欢默认 - 覆盖(或使用新实现复制)
-POST:参数:constructBodyWithBlock:success:failure:]
- 你想要做的不是立即开始
你的操作。 - 设置NSOperation依赖关系链
-
开始
第一个
- Subclass
AFHTTPRequestOperationManager
, optionally setting therequestSerializer
if you don't like the default - Override (or copy with new implementation)
-POST:parameters:constructingBodyWithBlock:success:failure:]
- what you want to do is NOTstart
your operation right away. - Set the NSOperation dependency chains
start
the first one
这篇关于AFNetworking可以处理请求队列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!