在我的通信课程完成之前,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。
我一定错过了一些基本的东西。任何帮助将不胜感激!
I have both a UILabel and UIProgressView that I'm giving multiple updates to, but nothing is rendered until my communication object is finished.
I've got this 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
The updateNow method calls AgentAssetManager:
[self scheduleInNextRunloopSelector:@selector(updateTime:)];
McDbg(m, d+1, @"Calling AgentAssetManager sendToServer");
// [statusLabel setText:@"Contacting Server"];
[agentAssetManager sendToServer:true taskController:self];
The AgentAssetManager sendToServer is performs a series of steps and sends notifications back to UpdateServerViewController via the TaskController protocol:
- (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 ....
Here are UpdateServerViewController taskSetProgress and 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];
}
The debug output clearly shows that UpdateServerViewController taskSetProgress and taskSetStatus are called when they are suppose to. The problem is that the new set values don't take effect until the entire agentAssetManager.updateNow method is complete.
Based on searches on this site for similar UILabel problems I added a timer method:
- (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"];
}
The problem is the same with or without the timer. With the timer, the debug shows that the timer is scheduled before agentAssetManager.updateNow is called, but the updateTime method is not called until agentAssetManager.updateNow finishes. This is even with a sleep(5) in updateNow to try to avoid a thread race condition.
Debugging shows that all of UpdateServerViewController and AssetAgentManager seems to run in thread 1. I don't do any NSRunLoop other than the above mentioned timer.
I must be missing something basic hear. Any help would really be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保在主线程中进行任何 ui 更改。
你可以尝试这样的事情:
Make sure to make any ui changes in the main thread.
You may try something like this: