logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

iOS - NSURLConnection로 다중 다운로드 구현

by digipine posted Nov 01, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
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.
}
 
TAG •

List of Articles
No. Subject Author Date Views
36 iOS - Socket Nagle 알고리듬 OFF digipine 2017.11.01 208
35 iOS - Thread Loop 내에서 UI 업데이트 방법 digipine 2017.11.01 211
34 iOS - UILabel 에서 AttributeString 사용하기 digipine 2017.11.02 640
33 iOS - UITextField의 Placeholder Color 색 변경하기 file digipine 2017.11.02 1152
32 iOS - View 이동 전환 하기 총정리 digipine 2017.11.01 537
31 iOS - 코드 수행시간 측정하기 - getTickCount digipine 2017.11.01 848
30 iOS,OSX - CFSocket 사용법 digipine 2017.11.01 444
29 macOS ARP Spoofing Attack file digipine 2020.09.17 694
28 macOS Daemon 관련 시스템 폴더 목록 lizard2019 2024.03.08 28
27 MacOS mysql 비밀번호 분실 시 재설정하기 digipine 2017.11.14 1029
26 OpenAL PDF, Sample Source file digipine 2017.11.02 796
25 OSX - Screen Serial Terminal - OSX에서 시리얼 터미널 사용하기 digipine 2017.11.03 6871
24 OSX 사파리 최신 버전 폰트 변경하기 file digipine 2017.11.03 1224
23 The distance estimate iBeacon signal strength lizard2019 2019.10.25 695
22 WatermelonDB 'jsi/jsi.h' file not found 문제 해결 file digipine 2021.04.06 732
21 XCode 8 업데이트 후 Code Sign Error 발생 시 해결법 1 digipine 2017.11.02 711
20 [iOS, MacOS] ATS 보안 정책 가이드 digipine 2017.11.02 570
19 [iOS, MacOS] NSArray 정렬 Sorting에 대해서 digipine 2017.11.02 667
18 [iOS, MacOS] NSNotification, NSNotificationCenter 사용법 digipine 2017.11.02 1026
17 [iOS, MacOS] Singleton 싱글톤 패턴 사용하기 2 digipine 2017.11.02 1080
Board Pagination Prev 1 2 3 Next
/ 3