PDA

View Full Version : How update label from within loop?




mrl72
Jan 26, 2011, 10:52 AM
I am currently pulling a JSON feed from my server and iterating through it. As I do this I'd like to update a label on the screen that shows the progress (eventually this will become a graphical progress bar). My current logic updates two global variables within the loop and then I have a refreshlabel method that is kicked off before the loop with repeats:YES. Within this method I simply reference the already created label by its tag and update the text accordingly by passing the global variables (which are just counts). What's happening though is the global variables are not being updated until the method that sets the global variables is complete. I think I'm on the right tracks but am missing something? I've also tried triggering the timer method from within the loop and just grabbing the progress using the userInfo property but this does not work either.

Cheers.



dejo
Jan 26, 2011, 11:01 AM
If your loop is running on the main thread, any UI changes are held off until you return control to the main run loop. Run your loop in the background and call out to the main thread to update your UI as needed.

PhoneyDeveloper
Jan 26, 2011, 11:03 AM
Drawing happens on the main thread after your code returns. So for an updated label to draw to the screen you need update its text and then return.

This is a common problem and there are a couple ways to do this. You could use a timer. Do a chunk of work in the timer callback and then update the label and return. Do some more work each time the timer callback is called until finished.

Probably a better way is to do your work in a background thread. You should update the progress from the background thread using performSelectorOnMainThread: Using NSOperation and NSOperationQueue is the best way to use background threads in most cases like this.

mrl72
Jan 26, 2011, 02:16 PM
If your loop is running on the main thread, any UI changes are held off until you return control to the main run loop. Run your loop in the background and call out to the main thread to update your UI as needed.

You are correct, the loop is in my main thread. So if I move this out with NStimer and display the label within the main thread, I can update the label from the loop within which is in NStimer?

Cheers.

dejo
Jan 26, 2011, 02:21 PM
You are correct, the loop is in my main thread. So if I move this out with NStimer and display the label within the main thread, I can update the label from the loop within which is in NStimer?

I'm going to recommend PhoneyDeveloper's second suggested approach:
Probably a better way is to do your work in a background thread. You should update the progress from the background thread using performSelectorOnMainThread: Using NSOperation and NSOperationQueue is the best way to use background threads in most cases like this.

mrl72
Jan 26, 2011, 02:29 PM
Ok thanks, I just tried the first method and it didn't work for me, the label just updated at the end of the loop.

Cheers.