2013-04-29 71 views
0

我有一個啓動更新過程的UIAlertView。
UIAlertView詢問用戶他們是否想要更新。帶線程的程序/方法流程

這裏是我的代碼:

- (void)reachabilityChanged:(NSNotification *)notification { 
    if ([connection isReachable]){ 
     [updateLabel setText:@"Connection Active. Checking Update Status"]; 
     [[[UIAlertView alloc] initWithTitle:@"Update Available" message:@"Your File Database is Out of Date. Would you like to Update?\nNote: Updates can take a long time depending on the required files." delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"Update Now", nil] show]; 
} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (buttonIndex == 1) { 
     [self updateFiles:[UpdateManager getUpdateFiles]]; 
    } 
} 

上面的代碼運行正常,但是,我updateFiles內:方法,我需要一些UI調整。

- (void)updateFiles:(NSArray *)filesList { 
    for (NSDictionary *file in filesList) { 
     [updateLabel setText:[NSString stringWithFormat:@"Downloading File: %@", [file objectForKey:@"Name"]]]; 
     [UpdateManager updateFile:[file objectForKey:@"File Path"]]; 
    } 
    [updateIndicator stopAnimating]; 
    [updateLabel setText:@"Update Completed"]; 
} 

的UIAlertView中並沒有消除,直到在updateFiles方法語句運行後。

我無法讓updateLabel顯示當前正在下載的文件,儘管在更新過程結束時,我們在標籤中獲得了「更新完成」。

任何人都可以幫忙嗎?

UPDATE

我開始懷疑這是更多數民衆贊成被一些重同步過程耽誤了進程。例如,我的[UpdateManager getUpdateFiles]方法很繁重,涉及從網絡獲取資源。同樣用我的[UpdateManager updateFile:[file objectForKey:@"File Path"]];方法。

有什麼辦法可以強制UI更新優先於這些方法嗎?

我只是想給用戶一些反饋意見。

回答

0

我找到了解決方案。

我無法更新UI並在同一線程上處理一些沉重的方法。
由於我只能更新主線程上的UI,我不得不做一些重新組織以確保進程在後臺線程上,但是隨後將UI更改提升爲主線程。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (buttonIndex == 1) { 
     [self performSelectorInBackground:@selector(updateFiles:) withObject:[UpdateManager getUpdateFiles]]; 
    } 
} 

- (void)updateFiles:(NSArray *)filesList { 
    for (NSDictionary *file in filesList) { 
     [updateLabel performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"Downloading File: %@", [file objectForKey:@"Name"]]]; 
     [UpdateManager updateFile:[file objectForKey:@"File Path"]]; 
    } 
    [updateIndicator stopAnimating]; 
    [updateLabel setText:@"Update Completed"]; 
} 

所以,我送updateFiles:背景和促進setText:和任何其他UI更改主線程。