ios - AFHTTPRequestOperationManager 是否在 operationQueue 上运行请求?

标签 ios iphone objective-c afnetworking

如果我创建了一个管理器并实例化了,我会这样做:

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

[operation start];

这会在管理器的 operationQueue 上运行吗?我似乎找不到任何保证它会使用 GET、POST、PUT 方法之一来代替我假设会将操作添加到队列的方法。

我在这里看到马特的回答,但我想确定其中一种方式。

How send request with AFNetworking 2 in strict sequential order?

我想做的是排队请求并通过设置让它们同步运行

[self.manager.operationQueue setMaxConcurrentOperationCount:1]; 

最佳答案

如果你想在队列上运行一个操作,你必须显式地将操作添加到队列中:

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

[queue addOperation:operation];

这假定您将创建一个队列:

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

如果您只是开始操作而不将它们添加到队列中(例如 [operation start]),它们将直接运行,而不考虑任何特定的队列参数。

请注意,您还可以将您的操作添加到操作管理器的队列中(您可以设置 maxConcurrentOperationCount,正如您在问题中提到的那样):

[self.manager.operationQueue addOperation:operation];

这使您不必创建自己的队列。但它随后假定您对该 manager 的所有其他 AFNetworking 请求也将连续运行。就个人而言,我会单独留下 manager.operationQueue,并为我需要串行操作的逻辑创建一个专用队列。

最后一点:使用串行网络操作会对并发请求造成显着的性能损失。我会根据您的问题假设您绝对需要连续执行此操作,但总的来说,我现在设计我的应用程序以尽可能使用并发请求,因为它是一个更好的用户体验。如果我需要串行执行特定操作(例如登录),那么我会使用操作依赖项或完成 block 来执行此操作,但应用程序的其余部分会尽可能运行并发网络请求。

关于ios - AFHTTPRequestOperationManager 是否在 operationQueue 上运行请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978951/

相关文章:

ios - Xcode Storyboard : Constraints don't let me put space between objects in a stack

ios - CoreBluetooth `retrieveConnectedPeripheralsWithServices` 返回断开连接的外围设备

iphone - 为什么 CLLocationManager 总是返回 true?

javascript - jquery Sticky Panel-v1.4.1 - iphone、ipad 修复吗?

ios - 检测App是生产版还是开发版

ios - 从 MSMessagesAppViewController 正确呈现 UIAlertController

ios - UIViewController 中的多个初始化步骤问题(不能使用多重继承)

iOS 图表未构建

iphone - 后台服务是 "allowed to continue running while in the background"。那为什么我不能让它工作呢?

ios - 将 NSString 解析为 NSDate