Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mrl72

macrumors regular
Original poster
Apr 20, 2008
221
19
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors regular
Original poster
Apr 20, 2008
221
19
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors regular
Original poster
Apr 20, 2008
221
19
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.