2013-07-26 2 views
0

저는 동시에 비동기 (델리게이트, 블록이 아님) 인 NSURLConnections을 실행 중이며, LAN 서버를 공격 할 때 매우 빠르게 돌아옵니다.NSURLConnection이 실행되지 않거나 시간이 초과되지 않습니다.

가끔씩 하나의 NSURLConnection은 사라지고 절대로 돌아 오지 않습니다.

connection:willSendRequest:이라고 부르지 만 connection:didReceiveResponse: (실패)이 아닙니다.

아이디어가 있으십니까? 대신 CFNetwork을 사용하여 간단한 드롭 인 대체를해야하는지 궁금합니다.

편집 : 표시 할 코드가 많지 않습니다. 내가 한 것은 파일을 다운로드하는 래퍼 클래스를 만든 것입니다. 나는 문제가 때 내가 별도의 대기열에 연결을 실행하지만, 일어날 것입니다 -하지만 여전히 발생합니다.

내가하고있는 일반적인 요점은 각 셀에 대한 다운로드 요청을 테이블 뷰 스크롤 (cellForRowAtIndexPath)에서 스크롤 한 다음 셀이 여전히 보이면 비동기 적으로 이미지 파일에서 이미지 파일로로드하는 것입니다. 요청한 바와 같이

_request = [NSMutableURLRequest requestWithURL:_URL]; 
_request.cachePolicy = NSURLRequestReloadIgnoringCacheData; 
_request.timeoutInterval = _timeoutInterval; 

if(_lastModifiedDate) { 
    [_request setValue:[_lastModifiedDate RFC1123String] forHTTPHeaderField:@"If-Modified-Since"]; 
} 


_connection = [[NSURLConnection alloc] initWithRequest:_request 
               delegate:self 
             startImmediately:NO]; 
[_connection start]; 

, 인스턴스 변수 :

NSMutableURLRequest *_request; 
NSURLConnection *_connection; 

위임 방법 :

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { 
    NSLog(@"%@ send", _URL); 
    return request; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"%@ response", _URL); 
    _response = (id)response; 
    // create output stream 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    _receivedLength += data.length; 
    _estimatedProgress = (Float32)_receivedLength/(Float32)_response.expectedContentLength; 
    [_outputStream write:data.bytes maxLength:data.length]; 

    // notify delegate 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // close output stream 
    // notify delegate 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"%@ failure", _URL); 
    // notify delegate 
} 

- (void)connection:(NSURLConnection *)connection 
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if(_credential && challenge.previousFailureCount == 0) { 
     [[challenge sender] useCredential:_credential forAuthenticationChallenge:challenge]; 
    } 
} 
+0

당신이 제발 당신 코드의 자세한 내용을 제공 할 수 있습니까? Btw, NSURLConnection도 비슷한 문제가 있습니다. 당신과 내 사이의 공통점을 찾을 수 있습니까 [여기] (http://stackoverflow.com/questions/17848290/nsurlconnection-doesnt-receive-data-when-creating-many-downloading-objects-in-i) – Envil

+0

표시하십시오 '_request'와'_connection'에 대한 선언, 그리고 당신의 델리게이트 메소드 (아래로 벗겨 낸). 방법으로 '대의원'은 무엇입니까? View Controller입니까, 다른 객체입니까? –

+0

이 토론에서는 본질적으로 '자기'입니다. 내 래퍼 개체 NSURLConnection 내 개체를 유지하지 않는 약한 프록시를 사용하는 옵션이 있습니다. 이 플래그는이 경우 꺼져 있습니다. – xtravar

답변

1

프로파일 러에서 파고 들자 리드를 발견하고 나에게 솔직하게 말했습니다.

내 자격 증명이 실패했습니다 (이유는 확실하지 않음). 따라서 previousFailureCount가 0이 아니므로 내 자격 증명 개체를 사용하지 않았습니다.

이에 코드를 변경하고 난 아무런 문제가 없다 :

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if(_credential) { 
     [[challenge sender] useCredential:_credential forAuthenticationChallenge:challenge]; 
    } 
} 
+0

이것은 정확하지 않습니다;) 먼저 어떤 종류의 인증이 필요한지 확인해야합니다. 보호 공간을 검색 한 다음 인증 방법을 검색합니다. 'NSURLAuthenticationMethodHTTPBasic'과'NSURLAuthenticationMethodHTTPDigest'에 대해서만 자격 증명을 제공합니다. – CouchDeveloper

0

NSURLConnection 보내는 것 중 didReceiveResponse 또는 didFailWithError.

가끔 didFailWithError이 발생하기 전에 시간 초과를 처리하고 있습니다.

+0

그게 설명서가 말하는 것입니다,하지만 그건 일어나지 않습니다. – xtravar

+0

@xtravar, 일부 코드를 보여줍니다. 나는 광범위하게 NSURLConnection을 사용했으며, 사용자 오류가 아닌 문제가 없었습니다.:) –

+0

나는 내가하고있는 일에 대해 더 자세히 설명했다. 솔직히, 나는 NSURLConnection을 사용하여 꽤 많은 일을 해왔으며 결코 이것에 부딪치지 않았다. 그러나 저는 이런 식으로 동시에 많은 것을 만들지 않았습니다. – xtravar

관련 문제