views:

97

answers:

1

I have an app with a tab bar that has a few tabs with user-generated content. When each view controller loads, the viewDidLoad method makes HTTP requests to a server to fetch data, then populates its view correspondingly. Once all the data is loaded, however, the view remains the same (unless the view controller is unloaded and reloaded).

A lot of other apps with user-generated content has a similar behavior, except each view will reload if the app goes into the background then comes back into the foreground again. That is, as the user switches between the various tabs while the app is active, the content won't be automatically refreshed. If the user "quits" the app then comes back to it later, the views will be reloaded.

Are there standard practices for designing an app to behave this way? Specifically, I'm wondering if there are UIKit niceties that I can take advantage of to refresh my views on app "restarts".

Otherwise, I guess a straightforward approach is to have each view controller register for notifications in viewDidLoad and listen for the app entering the foreground. The controller can then respond to each notification by reloading its data.

A: 

In each of the View Controllers, you want to subscribe to the UIApplicationDidBecomeActiveNotification notification, and implement a method that gets called when it receives that notification. This method will do the reloading however your data needs to be reloaded is really beyond the scope of what we can answer here. This is how I do it, and how I recommend doing it.

jer
An optimization to this is determining whether you are the visible VC. If you are not visible then simply set a dirty flag and refresh when and if you do become visible. viewDidAppear:(BOOL)animated will help you with your dirty flag implementation.
logancautrell
How do you determine if a view is visible? I think this is actually necessary, since always responding to UIApplicationDidBecomeActiveNotification will incur unnecessary costs to reload data for views that might not get shown. However, viewDidAppear: doesn't actually quite achieve what we need here, since it doesn't seem to fire when the app becomes active. It only seems to fire if the view appears while the app is _already active_, but not if the view is already visible when the app goes from inactive to active.
pmc255