답변

0

볼 그룹은 NSURLRequest 및 NSURLConnection입니다. 전자는 요청 (URL, http 메서드, 매개 변수 등)을 지정하고 후자는 요청을 실행합니다.

상태를 계속 업데이트하려면 (I answered a sketch of this in your other question), 연결에서 도착할 때 데이터 덩어리를 넘겨주는 NSURLConnectionDelegate 프로토콜을 구현해야합니다. 당신이 기대하는 데이터의 양을 알고 있다면, 당신은 내가 이전에 제안 양이 downloadProgress 플로트를 계산하기 위해받은 사용할 수 있습니다

float downloadProgress = [responseData length]/bytesExpected; 

여기 SO 일부 nice looking example code을합니다. 당신은

MyLoader.m

@interface MyLoader() 
@property (strong, nonatomic) NSMutableDictionary *connections; 
@end 

@implementation MyLoader 
@synthesize connections=_connections; // add a lazy initializer for this, not shown 

// make it a singleton 
+ (MyLoader *)sharedInstance { 

    @synchronized(self) { 
     if (!_sharedInstance) { 
      _sharedInstance = [[MyLoader alloc] init]; 
     } 
    } 
    return _sharedInstance; 
} 

// you can add a friendlier one that builds the request given a URL, etc. 
- (void)startConnectionWithRequest:(NSURLRequest *)request { 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    NSMutableData *responseData = [[NSMutableData alloc] init]; 
    [self.connections setObject:responseData forKey:connection]; 
} 

// now all the delegate methods can be of this form. just like the typical, except they begin with a lookup of the connection and it's associated state 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    NSMutableData *responseData = [self.connections objectForKey:connection]; 
    [responseData appendData:data]; 

    // to help you with the UI question you asked earlier, this is where 
    // you can announce that download progress is being made 
    NSNumber *bytesSoFar = [NSNumber numberWithInt:[responseData length]]; 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 
     [connection URL], @"url", bytesSoFar, @"bytesSoFar", nil]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyDownloaderDidRecieveData" 
     object:self userInfo:userInfo]; 

    // the url should let you match this connection to the database object in 
    // your view controller. if not, you could pass that db object in when you 
    // start the connection, hang onto it (in the connections dictionary) and 
    // provide it in userInfo when you post progress 
} 
+0

내 코드에서는 동시에 여러 다운로드를 처리하고 있으며 한 번의 클릭으로 파일 그룹을 다운로드하려고합니다. 그래서 나는 asinetworkqueue를 사용하고있다. 그래서 큐에 대한 downloadp 로스트 대리인을 설정 중입니다. – Mithuzz

+0

여러 번 같은 패턴입니다. 죄송합니다. 귀하의 질문에이 부분을 알리지 않았습니다. 편집 할 것입니다. – danh

0

내가 정확히 할 this 라이브러리를 쓴 ...이 같은 여러 개의 연결을 확장 할 수 있습니다. github 저장소에서 구현을 체크 아웃 할 수 있습니다.

관련 문제