简体   繁体   English

UIProgressView更新失败,并显示UINotification

[英]UIProgressView update fail with UINotification

I try to solve this problem for several days now I have to ask you... 我想解决这个问题好几天了,我不得不问你...

I've got a View (and a ViewController) with a UITableview. 我有一个带有UITableview的View(和一个ViewController)。 There is a TableViewController for that table which is generated in the ViewController. 该表有一个TableViewController,它是在ViewController中生成的。 The TableViewController calls a DataSyncManager sharedInstant object (which is obviously in a separate class) which starts to sync data with the server. TableViewController调用DataSyncManager sharedInstant对象(显然是在单独的类中),该对象开始与服务器同步数据。

I do it this way (first the refresh method): 我这样做(首先是刷新方法):

-(void) refresh{
    [serverQueueProgressView setProgress:0.0];
    [syncingLabel setAlpha:0.5];
    [serverQueueProgressView setAlpha:1];
    [self performSelector:@selector(reloadTableViewDataSource) withObject:nil afterDelay:1.0];
}

Then the method reloadTableViewDataSource (of TableViewController) is called: 然后调用TableViewController的reloadTableViewDataSource方法:

- (void)reloadTableViewDataSource
{
    [dataSyncManager getEntriesFromServer];
}

dataSyncManager is my sharedInstance. dataSyncManager是我的sharedInstance。

In the getEntriesFromServer method of dataSyncManager I do the loop with different sync items and call everytime 在dataSyncManager的getEntriesFromServer方法中,我使用不同的同步项进行循环并每次调用

[[NSNotificationCenter defaultCenter]
     postNotificationName:@"ServerQueueProgress"
     object:progress];

with the proper progress as NSNumber (that part works well). 取得与NSNumber一样的进度(该部分效果很好)。 The message is now sent and catched by my ViewController (it works, I checked with a breakpoint, it also gets the right progress-NSNumber and converts it to float): 现在,该消息已发送并由ViewController捕获(它可以正常工作,我在断点处进行了检查,它还获得了正确的progress-NSNumber并将其转换为float):

- (void)serverQueueProgress:(NSNotification *)notification {
    if(![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:_cmd withObject:notification waitUntilDone:NO];
        return;
    }

    [queueProgressView setProgress:[[notification object] floatValue]];
}

This is one solution which I found here on stackoverflow. 这是我在stackoverflow上找到的一种解决方案。 But the if is always skipped because obviously I'm on main thread. 但是if总是被跳过,因为显然我在主线程上。

Unfortunately the UIProgressview doesn't get updated, it just hangs around, but I connected it well in Interface Builder (I checked that by setting the progress in another method of ViewController. 不幸的是,UIProgressview没有更新,只是挂了,但是我在Interface Builder中连接得很好(我通过在ViewController的另一种方法中设置进度来检查它。

I also tried to catch the Notification with my TableViewController and put in some other solutions, but no chance, the UIProgressView doesn't get updated live. 我还尝试使用TableViewController捕获Notification并放入其他解决方案,但是没有机会,UIProgressView不会实时更新。 Only after the sync is done. 仅在同步完成后。

Here is the mentioned code in TableViewController which also gets executed without errors (I also stepped it to make sure every line gehts executed well): 这是TableViewController中提到的代码,它也可以无错误地执行(我也将其步进以确保每行代码都能正确执行):

This is the method called when received a the notification: 这是收到通知时调用的方法:

- (void)serverQueueProgress:(NSNotification *)notification {
    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[notification object] waitUntilDone:NO];

    [serverQueueProgressView setProgress:[[notification object] floatValue]];
}

Which also calls updateProgress: of the same class: 也可以调用同一类的updateProgress::

- (void)updateProgress:(NSNumber *)newProgressValue {
    [serverQueueProgressView setProgress:[newProgressValue floatValue]];
}

No chance. 没有机会。 I tried many ways and implemented some in parallel as you see, but the ProgressView won't get updated live. 如您所见,我尝试了许多方法并并行实现了一些方法,但是ProgressView不会实时更新。 Only at the end of syncing. 仅在同步结束时。 What am I doing wrong?? 我究竟做错了什么??

EDIT : Here is my getEntriesFromServer and some other stuff in DataSyncManager: 编辑 :这是我的getEntriesFromServer和DataSyncManager中的其他一些东西:

- (void)getEntriesFromServer
{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"SynchingStarted"
     object:nil];

    [self completeServerQueue];
    ...
}

and completeServerQueue is the function which sends messages to my ViewController with the proper progress float value (it's only a dummy for loop, which gets executed properly... I've checked it): 和completeServerQueue是使用适当的进度浮点值将消息发送到ViewController的函数(它只是for循环的虚拟对象,可以正确执行...我已经检查了它):

- (void)completeServerQueue {  
    NSNumber *progress = [[NSNumber alloc] init];
    for (int i=0; i<15; i++) {
        progress = [[NSNumber alloc] initWithFloat:(100/15*i) ];

        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"ServerQueueProgress"
         object:progress];

        sleep(1);
    }
}

my guess is the problem isn't the code you've shown here, but other code in getEntriesFromServer. 我的猜测是问题不在于您在此处显示的代码,而是getEntriesFromServer中的其他代码。 Are you using NSURLConnection? 您正在使用NSURLConnection吗? Something like: 就像是:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

then you will get callbacks asynchronously that you can use to update your progress view. 那么您将异步获得回调,可用于更新进度视图。

also, when you're having trouble, break the problem down a bit. 同样,当您遇到麻烦时,可以将问题分解一下。 Instead of: 代替:

[serverQueueProgressView setProgress:[[notification object] floatValue]];

do this; 做这个;

float prog = [notification object] floatValue];
[serverQueueProgressView setProgress:prog];

then debugging would give a clue that this isn't working. 然后调试将提供一个提示,表明这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM