How can I ensure my table data function is called only once when app is displayed again (from background start, or launch)?
That is, I want to have my table data refreshed when the user clicks on the application icon, and only once. So the requirement would be:
a) update method in controller called only once after application icon is clicked by user
b) to be valid irrespective of whether the user is (i) starting app for the first time, (ii) coming back from background, (iii) any other means of the user requesting the app come back to the foreground.
The issue I currently have with my implementation is that the data update is being called twice in the case where the app is coming back from background, so I basically want to improve on this. The way I’m doing currently, which is flawed is:
- (void)updateEventData {
// UPDATE CODE HERE
}
- (void)becomeActive:(NSNotification *)notification {
[self updateEventData];
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updateEventData];
// Auto Refresh Data - Handle Case where App becomes active from background & you want to refresh data
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
You can implement it in following way:
1) Take one variable in NSUserDefault.
2) Set it’s value FALSE when app is launch for the first time.
3) Get the value of NSUserDefault variable in respective view controller. And if it’s value if FALSE then call update method.
4) Now, when app become Active, then set the variable value TRUE.
5) Get it’s value in respective view controller. And if’s value is TRUE then don’t call update method.
6) So, update method will be called only once.
Hope it will be helpful to you. May be there is some minor changes.
Let me know in case of any difficulty.