2012-02-24 47 views
1

我有一个UILabel和UIProgressView,我给多个更新,但没有被呈现,直到我的通信对象完成。UILabel和UIProgressView没有更新,直到我的通信类完成

我有了这个视图控制器:

@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被调用之前计划,但updateAgent方法在agentAssetManager.updateNow完成之前不会被调用。这甚至在updateNow中有一个睡眠(5)来尝试避免线程竞争条件。

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

我必须缺少一些基本的听到。任何帮助真的会很感激!

+0

?如果您使用任何___withContentsOfURL方法,它们将阻止主线程,并且不会发生UI更新。 – jsd 2012-02-24 23:16:16

+0

我正在使用RestKit进行服务器通信。 – 2012-02-24 23:23:06

回答

0

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

你可以尝试这样的事:什么是你用来做实际的服务器通信

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

如果我建议你使用dispatch_sync(),那么整个线程挂起/阻塞。很奇怪。我开始认为RestKit(从AgentAssetManager运行)正在幕后做一些事情。 – 2012-02-25 22:05:06