NSURLConnection로 다중 다운로드 구현하는 방법입니다.
여러개의 다운로드를 동시에 실행하면 delegate method 에서 구분하지 못하는 문제가 발생합니다. 그래서 수정이 필요한데요.
아래 블로그에서 참조했습니다.
http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/
NSURLConnection 을 상속받는 CustomNSURLConnection 을 만든다.
그런뒤, 생성자에서 각 커넥션을 구분할수 있는 tag를 받고, 각 Connection 객체를 tag로 관리하는 것이다.
delegate method 인
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
를 보면 Connection을 argument로 받으니, CustomNSURLConnection 으로 casting 해서, tag값을 뽑아 구분한다는 이야기이다.
가장 심플하고 정확한 방법인 듯하다.
실제 소스를 보자,
NSURLConnection를 상속받는 CustomURLConnection 를 만들자.
[CustomURLConnection.h]
@interface CustomURLConnection : NSURLConnection {
NSString *tag;
}
@property (nonatomic, retain) NSString *tag;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag;
@end
implementation:
[CustomURLConnection.m]
@implementation CustomURLConnection
@synthesize tag;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag {
self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately];
if (self) {
self.tag = tag;
}
return self;
}
- (void)dealloc {
[tag release];
[super dealloc];
}
@end
header file에 각 connection에서 받은 데이터를 저장하기위해 NSMutableDictionary 를 선언한다.
NSMutableDictionary *receivedData;
- (void)startAsyncLoad:(NSURL*)url tag:(NSString*)tag {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];
if (connection) {
[receivedData setObject:[[NSMutableData data] retain] forKey:connection.tag];
}
}
- (NSMutableData*)dataForConnection:(CustomURLConnection*)connection {
NSMutableData *data = [receivedData objectForKey:connection.tag];
return data;
}
- (void)load {
receivedData = [[NSMutableDictionary alloc] init];
NSURL *url1 = [NSURL URLWithString:@"http://blog.emmerinc.be"];
NSURL *url2 = [NSURL URLWithString:@"http://www.emmerinc.be"];
NSURL *url3 = [NSURL URLWithString:@"http://twitter.com/emmerinc"];
[self startAsyncLoad:url1 tag:@"tag1"];
[self startAsyncLoad:url2 tag:@"tag2"];
[self startAsyncLoad:url3 tag:@"tag3"];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
[dataForConnection setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
[dataForConnection appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
[connection release];
// Do something with the dataForConnection.
}