How can we handle multiple NSURLConnection in iPhone Xcode?

只愿长相守 提交于 2019-11-30 07:35:59

Declare conn,conn1,conn2,conn3 in .h file. Then do the following. in loadTrafficAndEvent:

conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];

in connectionDidFinishDownloading: method,

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL{
if(connection==conn){
conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
}
else if(connection==conn1){
conn2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
}
else if(connection==conn2){
conn3 = [[NSURLConnection alloc] initWithRequest:request3 delegate:self];
}

}

Do the operations inside each if else condition, and no need to allocate and initialize all NSURLConnection in loadTrafficAndEvent: The download will occur one after other.

you might want to take a look at AFNetworking for an easier and tidier way of doing network requests.

Implement NSURLConnection delegate methods and rest will be handle by itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!