2013-04-29 73 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更改主线程。