views:

85

answers:

1

Hi,

I am experiencing an issue with activity indicator freezing when I move application to background and then bring it back to foreground. The application can be loading some data over the network when it is moved to background. Hence I display an activity indicator to the user. I have added the code to make sure that the task finishes in the background. When the application is brought to foreground immediately and the data is not completely loaded, the spinner stays on the screen but it stops spinning. When the data is completely loaded, the spinner disappears.

Any idea why the activity indicator freezes and what could be a possible solution to it.

Thanks.

+1  A: 

There's almost no reason for an app in the background to be updating its UI.

For one, who is there to see it? Another reason that the indicator freezes is that going to the background stops UI updates and animations that needlessly drain the device's battery and slow down the foreground app.

From Apple's excellent iPhone Application Programming Guide, the app delegate method -applicationWillEnterForeground: is called whenever the application comes into foreground.

You can override this method and add logic to wake up widgets here. Or you subscribe your class (view controller, etc.) to listen for the UIApplicationWillEnterForegroundNotification notification. If you listen for this notification, you could call a selector that updates the state of the UI, given whatever the application is doing at the time.

Alex Reynolds
Thanks Alex. One more question - when the application does come to foreground, it updates a tableview with the data fetched and hides the activity indicator. So if it wakes up the UI, why doesn't it wake up the activity indicator animation? Shouldn't it resume the spinning on foreground if it halted it on background?
Hetal Vora
The OS might not keep track of any animations it has halted when your app enters background -- it just wants to stop those UI updates. It is your responsibility, then, to update the state of the UI upon entering into foreground. The table view may be updated because you may have logic in `-viewDidAppear:` or the like that triggers the table view to reload its data.
Alex Reynolds
I am trying to make the spinner animate again in applicationWillEnterForeground by calling startAnimating on the activity indicator. But it does nothing. If I check isAnimating value, it displays TRUE. But the spinner is still frozen.
Hetal Vora
Perhaps you could try `-setNeedsDisplay` on the view controller's view, which contains the activity indicator as a subview. Or perhaps you have run into a bug. Can you reproduce this with a separate test application?
Alex Reynolds
Its working! Thanks a lot :)
Hetal Vora
This seems to be a bug. UIActivityView should automatically start animating again once it moves to the foreground again, but it doesn't. Calling startAnimating doesn't work because it believes itself in an animating state. stopAnimating -> startAnimating will return it to life though.
Nuoji