1

I'm trying to update a UIProgressview while loading data from a server. I have a Class with a sharedInstance to handle the server communication and there is a method which gets data from the server (in a while loop). Now I would like to update my UIProgressView by sending notifications from my server-communication-Class (sharedInstance), and it simply doesn't work.

The UIProgressView is correctly set in InterfaceBuilder and show/hide/setProgress works fine, but setProgress doesn't work through notification.

Here is the test-loop in my server-communication-Class:

- (void)completeServerQueue {

NSNumber *progress = [[NSNumber alloc] init];
for (int i=0; i<15; i++) {

    progress = [[NSNumber alloc] initWithFloat:(100/15*i) ];

    float test = [progress floatValue];

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

    sleep(1);
}

}

And this is the Method called when the notification is detected (I checked it with breakpoints, it is executed...):

- (void)serverQueueProgress:(NSNotification *)notification {
[serverQueueProgressView setProgress:[[notification object] floatValue]];

}

Any ideas?

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
Tamas Gal
  • 11
  • 2

2 Answers2

0

Is the code talking to the server on a background thread?

If so you may want to try doing:

[self performSelectorOnMainThread: @selector(updateProgress:) withObject: [notification object]];

You will then need this method:

- (void) updateProgress: (CGFloat) value
{
      [serverQueueProgressView setProgress: value];
}
JFoulkes
  • 2,429
  • 1
  • 21
  • 24
0

You are probably trying to update the progress from a separate thread which does not work properly. Try the following.

- (void)serverQueueProgress:(NSNotification *)notification {
    if(![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:_cmd withObject:notification waitUntilDone:NO];
        return;
    }
    [serverQueueProgressView setProgress:[[notification object] floatValue]];
}
Joe
  • 56,979
  • 9
  • 128
  • 135