Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIProgressView won't update even though main thread seems to be running fine

I have my main thread where I call a method which loads data (takes a while). I call this method with performSelectorInBackground and pass the delegate. The data loading method calls back regularly to update the progress, it calls a method in the same controller class that originally launched it in the background (the delegate). This method looks like:

-(void)loadingProgress:(float)progress{
    NSLog(@"Progress %f", progress);
    myProgressView.progress = progress;
}

So I know the method is being called and running because I get the log readout of the increasing progress values but the progress indicator doesn't move. Everything I have found has stated to make sure the main thread is free to update the view, but doesn't the fact that NSLog runs mean that it is free? What's going on?

like image 229
omsid Avatar asked Dec 21 '22 17:12

omsid


1 Answers

you have to update userinterface elements on the main thread. And therefor you have to change your method a little bit, because you have to use objects when using performSelectorOnMainThread:withObject:waitUntilDone:

Your method should look like this:

-(void)loadingProgress:(NSNumber *)nProgress{
    float progress = [nProgress floatValue];
    NSLog(@"Progress %f", progress);
    myProgressView.progress = progress;
}

And you call it with:

[delegate performSelectorOnMainThread:@selector(loadingProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
like image 124
Matthias Bauch Avatar answered Feb 22 '23 23:02

Matthias Bauch