如何管理多个异步NSURLConnection的要求 [英] How to manage multiple asynchronous NSURLConnection request
问题描述
我正在对应用程序启动和退出appdelegate.m 3 NSURLConnection的请求,我使用异步调用,返回的响应是字典的数组,在所有响应具有相同名称的数组,所以我不能用钥匙检查。
I am making 3 NSURLConnection requests on app start and exit in appdelegate.m, I am using asynchronous call, the response returned is array of dictionaries, with array having same name in all responses, so I cannot check with key.
于是,我就在宣布与appdelgate.h然后connectionDidFinishloading比较了连接放慢参数
So I tried declaring NSURLConnection variables in appdelgate.h and then in connectionDidFinishloading compared each declared connection object with connection paramter
// Appdelegate.h
//Appdelegate.h
NSURLConnection *conn1;
NSURLConnection *conn2;
NSURLConnection *conn3;
然后//Appdelgate.m
then in //Appdelgate.m
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xyz/login"]];
request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"GET"];
conn1 = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[conn1 start];
}
然后
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *e = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData: m_responseData options: 0 error: &e];
//Extracy spcific keys and add to respective arrays
if(connection == conn1)
{
//call 2 nd request
}
if(connection == conn2)
{
//call 3 rd request
}
}
我检查连接对象的信息,它总是显示我这个
I checked info of connection object, it always shows me this
<NSURLConnection: 0x14d09c20> { request: <NSMutableURLRequest: 0x14d0ac70> { URL: http://xyz/login } }
甚至称第二次请求后它仍然显示我第一次请求。
Even after calling second request it still shows me first request.
所以比较失败。
那么什么可以为这个更好的方法呢,还是我失去了一些东西。
So what can be better approach for this, or is it I am missing something.
推荐答案
使用,就像
NSURLConnection *itemIdConnection,*contactNameConnection;
NSMutableData *receivedData, *locationData;
和您的委托方法是
#pragma NSUrlConnectionDelegate Methods
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if(connection == itemIdConnection)
{
if (receivedData == NULL)
{
receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];
}
if(connection == contactNameConnection)
{
if (locationData == NULL)
{
locationData = [[NSMutableData alloc] init];
}
[locationData setLength:0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (connection==itemIdConnection) {
[receivedData appendData:data];
}
if(connection == contactNameConnection)
{
[locationData appendData:data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *customAlert = [[UIAlertView alloc]initWithTitle:@"No NetWork" message:@"Interet Connection is Lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[customAlert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection==itemIdConnection) {
// NSError *e = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData options: kNilOptions error:nil];
NSString *tmp=[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", tmp);
NSLog(@" parsing JSON: %@", jsonDict);
}
if (connection==contactNameConnection) {
NSError *e = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:locationData options: NSJSONReadingMutableContainers error: &e];
NSLog(@" parsing JSON: %@", jsonDict);
}
}
这篇关于如何管理多个异步NSURLConnection的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!