串行启动 NSURLConnection 请求 [英] Start NSURLConnection requests serially
问题描述
我正在遍历一个包含几个字符串的数组,并为每个字符串向 Web 服务器发出请求数组中的字符串.
I'm looping through an array which contains few strings and making a request to a web server for each strings in the array.
我希望在将后续请求发送到服务器之前完全处理每个请求.因为每个请求都会向我发送一个响应,我将在下一个请求中发送该响应等等.
I would like each request to be processed completely before the subsequent request is sent to the server. Because each request sends me a response which I will send with next request and so on.
我遇到的问题是我的 NSURLConnection 是使用标准异步调用设置的.这导致请求不会阻塞任何后续请求.但是我需要在第一次完成之前阻止循环中的其他请求.
The problem I am having is that my NSURLConnection is set up using the standard asynchronous call. This results in requests not blocking any subsequent requests. But I need to block other requests in the loop before first completes.
请求 URL 始终相同,只有 JSON 数据随循环中的每个请求而变化.这是我的代码
The request URL is same always , only JSON data changes with every request in the loop. Here is my code
for (int i = 0; i < array.count; i++)
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalJSON options:NSJSONWritingPrettyPrinted error:&error];
if (!jsonData) {
NSLog(@"Error creating JSON object: %@", [error localizedDescription]);
}
NSString *url = [NSString stringWithFormat:@"abc.com/folders"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:APIKEY forHTTPHeaderField:@"X_API_KEY"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
//I am adding all connections to NSDictionary so that later I can process request.
NSURLConnection *connection = [self connectionForRequest:request];
[connection start];
}
推荐答案
我为你的问题想到了 2 个解决方案:
I thought of 2 solutions for your problem:
AFNetworking - 你可以使用
AFNetworking
并维护一个计数器在成功块中.计数器将对请求进行计数,当所有请求完成后,将执行您的下一个任务.
AFNetworking - U can use
AFNetworking
and maintain a counter in the success block. The counter will count the requests and when all done, will do your next task.
GCD - 调度组 - Grand Central Dispatch 为您提供创建组或请求并在最后(当所有请求完成时)做某事的选项.为此,你需要阅读很好的教程(Ray Wenderlich"的第二部分.如果你不熟悉 GCD,请跳到教程的第一部分).
GCD - Dispatch Groups - Grand Central Dispatch provide u the option to make group or requests and do something at the end (when all the requests finished). For that, u need to read nice tutorial (2nd part of "Ray Wenderlich". If U r not familiar with GCD, jump to the tutorial 1st part).
无论如何,使用上面的代码你无法完成你的任务.你没有任何在请求结束时运行的异步块.
Anyway, With your code above U can't achieve your task. U don't have any async block which run at the end of the requests.
使用 AFNetworking:
Use AFNetworking:
你必须先删除你的 for 循环,然后这样做:
U must remove your for loop first, and then do like this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { // HERE u can do your second request which uses the first response
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters_new = <USE_YOUR_DATA_FROM_responseObject>;
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { // HERE u can do your third request which uses the first and second response
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这篇关于串行启动 NSURLConnection 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!