2011-09-12 1 views
0

내 AppDelegate에 다음 메소드가 있습니다. updateView에서 라벨을 업데이트하고 ASIHTTPRequest를 통해 업로드를 수행하는 방법을 찾을 수 없습니다. 아래 코드에서 업로드 (startUpload)는 수행하지만 레이블 (updateLabel)은 업데이트하지 않습니다. startUpload에 대한 호출을 주석 처리하면 레이블이 업데이트됩니다. 또한 uploadViewController 내에서 디스패치 큐를 만들려고했는데 비슷한 결과가 있었는데 두 메서드 중 하나만 작동 할 수있었습니다. UploadViewController에서dispatch_async를 사용할 때 문제가 발생합니다. 동일한 컨트롤러에서 두 가지 함수 호출을 실행할 수 없습니까?

- (void) showProgressView:(NSString *)filePath { 
    NSView *view = [uploadViewController view]; 
    dispatch_queue_t queue = dispatch_get_global_queue(0,0); 
    dispatch_async(queue, ^{ 
     // assigns the upload view to the root NSPanel 
     [box setContentView:view]; 
    }); 
    dispatch_async(queue, ^{ 
     // starts the upload using ASIHTTPRequest 
     [uploadViewController startUpload:filePath]; 
    }); 
    dispatch_async(queue, ^{ 
     // updates a label in the upload view 
     [uploadViewController updateLabel]; 
    }); 
} 

:

-(void)startUpload:(NSString *)filePath { 
    HandZipper *handZipper = [HandZipper alloc]; 
    zipPath = [handZipper compressHands:(filePath):(processStatus)]; 

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ignore"]] autorelease]; 

    [request setRequestMethod:@"PUT"]; 
    [request setPostBodyFilePath:zipPath]; 
    [request setShouldStreamPostDataFromDisk:YES]; 
    [request setDelegate:self]; 
    [request setUploadProgressDelegate:progressIndicator]; 
    [request setDidFinishSelector:@selector(postFinished:)]; 
    [request setDidFailSelector:@selector(postFailed:)]; 
    [request startSynchronous]; 

} 

답변

2

당신은 항상 주 스레드에서 UI 요소를 업데이트해야합니다. showProgressView:이 주 스레드에서 호출된다는 것을 알고 있다면 [box setContentView:view];[uploadViewController updateLabel];에서 dispatch_async를 제거하고 직접 호출하십시오. 그렇지 않으면 showProgressView:이 다른 스레드에서 호출 될 수 있다면 queue 대신 dispatch_get_main_queue(void)을 사용하면됩니다.

+0

ty, ty. 잘 설명하고 효과가있었습니다. –

관련 문제