2011-09-12 42 views
0

我在我的AppDelegate中有以下方法。我無法找到更新updateView上的標籤並通過ASIHTTPRequest執行上傳的方法。在下面的代碼中,它將執行上載(startUpload),但不更新標籤(updateLabel)。如果我註釋掉對startUpload的調用,它將更新標籤。我也嘗試在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]; 
    }); 
} 

在UploadViewController:

-(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:可能從另一個線程調用,那麼只需使用dispatch_get_main_queue(void)而不是queue

+0

ty,ty。很好解釋和它的工作。 –

相關問題