简体   繁体   English

AFHTTPRequestOperationManager是否在operationQueue上运行请求?

[英]Does AFHTTPRequestOperationManager run requests on the operationQueue?

If I have a manager created and instance-varred and I do: 如果我有一个经理创建和实例变量,我做:

AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request success:mySuccessBlock failure:myFailureBlock];

[operation start];

will this run on the manager's operationQueue? 这会在经理的operationQueue上运行吗? I can't seem to find anything guaranteeing it will verse using one of the GET, POST, PUT methods instead which I assume will add the operation to the queue. 我似乎无法找到任何保证它将使用GET,POST,PUT方法之一,而我假设将把操作添加到队列。

I see mattt's answer here, but I want to be sure one way or the other. 我在这里看到了mattt的答案,但我想确定这种或那种方式。

How send request with AFNetworking 2 in strict sequential order? 如何使用AFNetworking 2以严格的顺序发送请求?

What I'm trying to do is queue up requests and have them run synchronously by setting 我要做的是排队请求并让它们通过设置同步运行

[self.manager.operationQueue setMaxConcurrentOperationCount:1]; 

If you want to run an operation on a queue, you have to explicitly add the operation to the queue: 如果要对队列运行操作,则必须将操作显式添加到队列:

AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request success:mySuccessBlock failure:myFailureBlock];

[queue addOperation:operation];

This presumes that you would create a queue: 这假设你要创建一个队列:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.name = @"com.domain.app.networkQueue";
queue.maxConcurrentOperationCount = 1;

If you just start the operations without adding them to a queue (eg [operation start] ), they'll just run, without honoring any particular queue parameters. 如果你只是在没有将它们添加到队列的情况下启动操作(例如[operation start] ),它们就会运行,而不会遵循任何特定的队列参数。

Note, you could also have added your operation to the operation manager's queue (which you can set the maxConcurrentOperationCount , as you alluded to in your question): 注意,您也可以将操作添加到操作管理器的队列中(您可以设置maxConcurrentOperationCount ,正如您在问题中提到的那样):

[self.manager.operationQueue addOperation:operation];

That saves you from having to create your own queue. 这使您无需创建自己的队列。 But it then presumes that all of your other AFNetworking requests for this manager would also run serially, too. 但它假设您对该manager 所有其他AFNetworking请求也将连续运行。 Personally, I'd leave manager.operationQueue alone, and create a dedicated queue for my logic that required serial operations. 就个人而言,我将单独留下manager.operationQueue ,并为我需要串行操作的逻辑创建一个专用队列。

One final note: Using serial network operations imposes a significant performance penalty over concurrent requests. 最后一点:使用串行网络操作会对并发请求造成严重的性能损失。 I'll assume from your question that you absolutely need to do it serially, but in general I now design my apps to use concurrent requests wherever possible, as it's a much better UX. 我会从你的问题中假设你绝对需要按顺序进行,但总的来说我现在设计我的应用程序以尽可能使用并发请求,因为它是一个更好的用户体验。 If I need a particular operation to be serial (eg the login), then I do that with operation dependencies or completion blocks, but the rest of the app is running concurrent network requests wherever possible. 如果我需要一个特定的操作是串行的(例如登录),那么我用操作依赖或完成块来做,但应用程序的其余部分尽可能运行并发网络请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM