如何用AFNetworking 2批量请求?

所以我正在用AFNetworking 2.0重写一个iOS 7的应用程序,而且我正在考虑一次发送一批请求并跟踪他们的进度。 在旧的AFNetworking中有enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:方法,这显然是重构出来的,我对如何排队多个请求感到困惑。

我创build了AFHTTPSessionManager的子类,并使用POST:...GET:...方法与服务器进行通信。 但是在代码和/或文档中,我找不到像旧AFHTTPClient一样AFHTTPClient多个请求的AFHTTPClient

我唯一能find的是在batchOfRequestOperations:progressBlock:completionBlock:上未batchOfRequestOperations:progressBlock:completionBlock:方法,但看起来像这样做的iOS 6方法。

很明显,我在新的NSURLSession概念中缺less一些东西,我应该使用批量请求或查看新的AFNetworkingfunction。 希望有人能帮助我在正确的轨道上!

tl; dr:如何用AFHTTPSessionManager子类发送一批请求?

感谢Sendoa的链接到GitHub的问题,在这里Mattt解释了为什么这个function不再工作。 为什么在新的NSURLSession结构中这是不可能的NSURLSession ? 任务不是操作,所以使用依赖或批处理的旧方法将不起作用。

我已经使用dispatch_group创build了这个解决scheme,可以使用NSURLSession对请求进行NSURLSession ,这里是(伪)代码:

 // Create a dispatch group dispatch_group_t group = dispatch_group_create(); for (int i = 0; i < 10; i++) { // Enter the group for each request we create dispatch_group_enter(group); // Fire the request [self GET:@"endpoint.json" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { // Leave the group as soon as the request succeeded dispatch_group_leave(group); } failure:^(NSURLSessionDataTask *task, NSError *error) { // Leave the group as soon as the request failed dispatch_group_leave(group); }]; } // Here we wait for all the requests to finish dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // Do whatever you need to do when all requests are finished }); 

我想看看写些什么,这使得这更容易做,并与马特讨论,如果这是什么东西(当很好地实现),可以合并到AFNetworking。 在我看来,用图书馆本身做这样的事情会很棒。 但是我必须检查什么时候有空。

只是更新线程…我有同样的问题,并经过一些研究,我发现了一些很好的解决scheme,但我决定坚持这一个:

我正在使用称为螺栓的项目。 所以,对于@ Mac_Cain13发布的上面同样的示例,它将是:

 [[BFTask taskWithResult:nil] continueWithBlock:^id(BFTask *task) { BFTask *task = [BFTask taskWithResult:nil]; for (int i = 0; i < 10; i++) { task = [task continueWithBlock:^id(BFTask *task) { return [self executeEndPointAsync]; }]; } return task; }] continueWithBlock:^id(BFTask *task) { // Everything was executed. return nil; }];; - (BFTask *) executeEndPointAsync { BFTaskCompletionSource *task = [BFTaskCompletionSource taskCompletionSource]; [self GET:@"endpoint.json" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { [task setResult:responseObject]; } failure:^(NSURLSessionDataTask *task, NSError *error) { [task setError:error]; }]; }]; return task.task; } 

基本上,它是堆叠所有的任务,等待和展开,直到没有更多的任务,并在一切完成后,最后一个完成块被执行。

另一个做同样事情的项目是RXPromise,但是对于我来说, Bolts的代码更清晰。

对于可以postget request ,可以使用AFNetworking 2.0进行批量操作,首先需要创build如下操作:

 //Request 1 NSString *strURL = [NSString stringWithFormat:@"your url here"]; NSLog(@"scheduleurl : %@",strURL); NSDictionary *dictParameters = your parameters here NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:strURL parameters:dictParameters error: nil]; AFHTTPRequestOperation *operationOne = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operationOne = [AFHTTPResponseSerializer serializer]; [operationOne setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //do something on completion } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@",[error description]); }]; //Request 2 NSString *strURL1 = [NSString stringWithFormat:@"your url here"]; NSLog(@"scheduleurl : %@",strURL); NSDictionary *dictParameters1 = your parameters here NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:strURL1 parameters:dictParameters1 error: nil]; AFHTTPRequestOperation *operationTwo = [[AFHTTPRequestOperation alloc] initWithRequest:request1]; operationTwo = [AFHTTPResponseSerializer serializer]; [operationTwo setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //do something on completion } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@",[error description]); }]; //Request more here if any 

现在执行像这样的批处理操作:

 //Batch operation //Add all operation here NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[operationOne,operationTwo] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) { NSLog(@"%i of %i complete",numberOfFinishedOperations,totalNumberOfOperations); //set progress here yourProgressView.progress = (float)numberOfFinishedOperations/(float)totalNumberOfOperations; } completionBlock:^(NSArray *operations) { NSLog(@"All operations in batch complete"); }]; [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO]; 

在AFNetworking 2.0上, AFHTTPClient已经在AFHTTPRequestOperationManagerAFHTTPSessionManagerAFHTTPRequestOperationManager ,所以可能你可以从第一个开始,它有operationQueue属性。

目前, NSURLSession任务不适合于相同types的模式请求操作使用。 在这里看到Mattt Thompson的回答。

直接回答:如果您需要依赖关系或批次,您仍然需要使用请求操作。