UILabel和UIProgressView在我的通信课程完成之前不会更新

时间:2012-02-24 20:54:49

标签: ios uilabel uiprogressview

我有一个UILabel和UIProgressView,我正在进行多次更新,但在我的通信对象完成之前不会呈现任何内容。

我有这个ViewController:

@interface UpdateServerViewController : UIViewController <TaskController> {
    AgentAssetManager * agentAssetManager;
}

@property (strong, nonatomic) IBOutlet UIButton * updateNowButton;
@property (strong, nonatomic) IBOutlet UILabel * statusLabel;
@property (strong, nonatomic) IBOutlet UILabel * lastUpdateSuccessTimeLabel;
@property (strong, nonatomic) IBOutlet UILabel * lastUpdateAttemptTimeLabel;
@property (strong, nonatomic) IBOutlet UILabel * lastUpdateResultLabel;
@property (strong, nonatomic) IBOutlet UIProgressView * progressView;

- (IBAction) updateNow:(id) sender;
- (void) updateLabels;
- (void) scheduleInNextRunloopSelector:(SEL)selector;
- (void) taskSetStatus:(NSString *)status;
- (void) taskFinishedSuccessfully;
- (void) taskFinishedUnSuccessfully;

@end

updateNow方法调用AgentAssetManager:

[self scheduleInNextRunloopSelector:@selector(updateTime:)];

 McDbg(m, d+1, @"Calling AgentAssetManager sendToServer");
//    [statusLabel setText:@"Contacting Server"];
[agentAssetManager sendToServer:true taskController:self];

AgentAssetManager sendToServer执行一系列步骤,并通过TaskController协议将通知发送回UpdateServerViewController:

- (void) sendToServer:(Boolean) notifyUser 
   taskController:(id<TaskController>) taskCtlr
{
char *          m = "AgentAssetManager.sendToServer";
int             d = 0;
int             steps = 6; // Total number of steps

McDbg(m, d, @"Send Asset data to server");
McDbg(m, d+1, @"Notify User about status/errors=%s",
      (notifyUser) ? "YES" : "NO");

if (taskCtlr != nil) {
    [taskCtlr taskSetProgress:(float)1/(float)steps];
    [taskCtlr taskSetStatus:@"Checking if server is reachable"];
}

McDbg(m, d+1, @"SLEEPING");
sleep(5); // XXX tmp debug

ServerStatusManager *statusMgr = [[ServerStatusManager alloc] init];
Boolean ready = [statusMgr isServerReady];
McDbg(m, d+1, @"Server Status ready=%s", (ready) ? "YES" : "NO");
if (!ready) {
    NSString *msg = @"Server could not be reached";
    McLogWarn(m, @"Server Not ready %@", [statusMgr serverUrlBase]);
    [self updateResult:false];
    if (notifyUser)
        [McUiError showMessage:msg];
    if (taskCtlr) {
        [taskCtlr taskSetStatus:msg];
        [taskCtlr taskFinishedUnSuccessfully];
    }
    return;
}

McDbg(m, d+1, @"Getting Asset data");
if (taskCtlr != nil) {
    [taskCtlr taskSetProgress:(float)2/(float)steps];
    [taskCtlr taskSetStatus:@"Gathering data"];
}

... etc ....

这是UpdateServerViewController taskSetProgress和taskSetStatus:

- (void) taskSetStatus:(NSString *) status
{
char *          m = "UpdateServerViewController.taskSetStatus";
int             d = 0;

McDbg(m, d, @"Status=<%@>", status);
[statusLabel setText:status];
McDbg(m, d+1, @"Finished");
}

- (void) taskSetProgress:(float) percent
{
[progressView setHidden:false];
[progressView setProgress:percent animated:YES];
}

调试输出清楚地显示UpdateServerViewController taskSetProgress和taskSetStatus在它们被假设时被调用。问题是在整个agentAssetManager.updateNow方法完成之前,新的设置值才会生效。

根据在此网站上搜索类似的UILabel问题,我添加了一个计时器方法:

- (void) scheduleInNextRunloopSelector:(SEL)selector 
{
char *          m = "UpdateServerViewController.scheduleInNext";
int             d = 0;

McDbg(m, d, @"Starting");
NSDate *fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0.5]; // 500 ms
NSTimer *timer = [[NSTimer alloc]
                  initWithFireDate:fireDate interval:0.5 target:self
                  selector:selector userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

}

- (void)updateTime:(NSTimer *)timer 
{
char *          m = "UpdateServerViewController.updateTime";
int             d = 0;

McDbg(m, d, @"Starting");
[statusLabel setText:@"XXX Timer called"];
}

无论有没有计时器,问题都是一样的。使用计时器,调试显示在调用agentAssetManager.updateNow之前调度计时器,但在agentAssetManager.updateNow完成之前不会调用updateTime方法。即使在updateNow中使用sleep(5)也可以尝试避免线程争用情况。

调试显示所有UpdateServerViewController和AssetAgentManager似乎都在线程1中运行。除了上面提到的计时器之外,我没有做任何NSRunLoop。

我必须遗漏一些基本听到的东西。真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

确保在主线程中进行任何ui更改。

您可以尝试这样的事情:

dispatch_sync(dispatch_get_main_queue(), ^{
        [statusLabel setText:@"XXX Timer called"];
    }
);