나는 서버에 요청을 많이 할 : 요청이 완료되면, 내가 콜백을 얻을 이것이 백그라운드 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];
});
});
}
제 질문은 두 가지입니다.
올바른 방법인가요? 콜백이 mainthred에서 실행되도록 강제해야합니까, 아니면 코드의 논리가 잘못 되었습니까?
콜백을 강제로 실행해야만 위 코드가 올바른지 확인하십시오.
, 상기 때로는 서버 요청 코드 충돌 :
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
그리고이 경고 일도 얻을 : 사전에
Class _NSZombie_NSRunLoop is implemented in both ?? and ??
감사
Pelase 안녕 –
기능 "업데이트"의 코드를 게시해야 할 UI 스레드에서 호출 할 수있다 , 응답 주셔서 감사합니다. 업데이트 기능은 무엇이든 가능합니다. 지금은 그냥 빈 함수입니다. 나는 NSLog를 넣었고 결코 호출되지 않았습니다. – BlackMouse
"// resultArray data를 처리합니다"는 UI 또는 일부 공유 저장 영역을 처리합니까? –