1

I am showing an Activity indicator in welcome screen as well as main screen. It is working fine in welcome screen. In main view, there is a 'for' loop code which does some task for 16 times. At this time, device looks like a bit hang, to avoid that i want to show activity indicator. When i add the standard activity code in main screen during some loop(16 times) code going on, i'm not observing the set activity indicator show. It comes only after the loop finished. I tried giving some delay before doing loop code, but still doesn't show during expected time. Otherwise user may think that my app hangs the device, so i want to show activity indicator at that time.

Any suggestions to resolve this are welcome !

thanks.

Marc W
  • 19,083
  • 4
  • 59
  • 71
Getsy
  • 4,887
  • 16
  • 78
  • 139

2 Answers2

6

What happens is, when your program blocks the main run loop, UIKit does not call start on the indicator until your code is done. The way to get around this is, make your loop run in a background thread by using the:

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

method on NSObject, this should take care of your problem.

Heres a link to the docs on the method:

NSObject Reference

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
Daniel
  • 22,363
  • 9
  • 64
  • 71
1

UI elements only update at the completion of the current run loop. If you are adding and starting your activity indicator in the same thread as where you are running your for loop with the 16 computations, the indicator won't update until those computations are complete since the current run loop can't finish before that point.

What you should do is add and start your activity indicator in your main thread (as you are probably doing right now) and then kick off your for loop in a background thread. Post a notification when the work in that background thread is complete, and at that point (back in the main thread) you can stop and remove the activity indicator.

Marc W
  • 19,083
  • 4
  • 59
  • 71
  • I have a similar question at http://stackoverflow.com/questions/5151521/calling-a-function-on-a-background-thread-ios-what-happens-to-the-nested-function Please advise. – Namratha Mar 01 '11 at 07:34
  • http://stackoverflow.com/questions/4253521/performselectorinbackground-and-performing-ui-operation Here, the person says that UI action is not allowed with the - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg Please clarify. I'm confused. – Namratha Mar 01 '11 at 07:35