2012-06-02 5 views
0

나는 서버에 요청을 많이 할 : 요청이 완료되면, 내가 콜백을 얻을 이것이 백그라운드 thred를 처리하는 올바른 방법입니까?

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    [theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; 

    urlData = [[NSMutableData data] retain]; 
    urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; 

    if (urlConnection) 
    { 
     finishedLoading = FALSE; 
     while(!finishedLoading) { 
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
     } 
    } 
    else 
    { 
     [delegate swHttpConnection:self withRequest:urlString failedWithError:nil]; 
    } 

.... 그러나 아래 코드를 사용하면 선택기가 호출되지 않습니다.

- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray 
{ 
    //Handle resultArray data 

    [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
} 

..하지만 아래 코드를 사용하면; 잘 작동합니다.

- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^(void) 
     { 
      //Handle resultArray data 

      [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
     }); 
    }); 
} 

제 질문은 두 가지입니다.

  1. 올바른 방법인가요? 콜백이 mainthred에서 실행되도록 강제해야합니까, 아니면 코드의 논리가 잘못 되었습니까?

  2. 콜백을 강제로 실행해야만 위 코드가 올바른지 확인하십시오.

또한

, 상기 때로는 서버 요청 코드 충돌 :

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 

그리고이 경고 일도 얻을 : 사전에

Class _NSZombie_NSRunLoop is implemented in both ?? and ?? 

감사

+0

Pelase 안녕 –

+0

기능 "업데이트"의 코드를 게시해야 할 UI 스레드에서 호출 할 수있다 , 응답 주셔서 감사합니다. 업데이트 기능은 무엇이든 가능합니다. 지금은 그냥 빈 함수입니다. 나는 NSLog를 넣었고 결코 호출되지 않았습니다. – BlackMouse

+0

"// resultArray data를 처리합니다"는 UI 또는 일부 공유 저장 영역을 처리합니까? –

답변

1

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^(void) 
     { 
      //Handle resultArray data 

      [self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f]; 
     }); 
    }); 

다음은 네 당신이

2

그렇게하지를 NSURLConnectionDelegaterequest:finished: 방법이 있다고 생각하십니까? 코드를보고 MBRequest을 사용하여 솔기를 잡습니다. 그렇다면 블록을 사용하여이를 수행하는 방법과 같은 웹 페이지를 살펴 봐야한다고 강력히 생각합니다. 이 세그먼트의 UI를 업데이트 할

관련 문제