Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Respond to applicationWillEnterForeground in a UIView

Tags:

iphone

ios4

I have a UITableView that loads remote data from the network. I have logic in viewWillAppear to handle refreshing the data when needed based on state changes and a TTL for the data set. This works fine within an app "session" but isn't effective when the user exits the app to the background and later restores it directly to this view.

What I'm finding is that when the app is restored to the foreground in iOS4 - viewWillAppear is not invoked in the view- I can totally understand why this is by design. Is there another UIView delegate I should be taking advantage of? I didn't spot anything in the docs.

Since willEnterForeground is available in the app delegate is the best way to handle this firing a notification from the app delegate to the view? Am I missing a more direct an elegant way of catching this in a UIView?

Thanks for any tips.

like image 902
Nick Avatar asked Aug 10 '10 00:08

Nick


1 Answers

You are correct about the delegate calls, though for what you are doing it sounds to me like you will want to suspend and resume your network activity based on the methods applicationDidBecomeActive: and applicationWillResignActive: which are both on the app delegate.

In answer to your question about a more direct way of catching these notifications in your UIView, if you do not have convenient access to the app delegate you could register for notification of UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification via the default notification center. That somewhat detaches you from coupling the app delegate to your view.

like image 196
jbm Avatar answered Oct 20 '22 00:10

jbm