如何确保当应用程序再次显示(从后台启动或启动)时,我的 UITableView 数据更新函数仅被调用一次?
如何确保当应用程序再次显示时(从后台启动或启动)仅调用一次我的表数据函数?
也就是说,我希望当用户单击应用程序图标时刷新表数据,并且仅刷新一次。所以要求是: a) 在用户单击应用程序图标后仅调用一次控制器中的更新方法 b) 无论用户是否 (i) 首次启动应用程序、(ii) 从后台返回、(iii) 用户请求应用程序返回前台的任何其他方式,都有效。
我目前在实现中遇到的问题是,在应用程序从后台返回的情况下,数据更新被调用两次,所以我基本上想对此进行改进。我目前正在做的有缺陷的方法是:
- (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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过以下方式实现它:
1)在 NSUserDefault 中取一个变量。
2)当应用程序第一次启动时,将其值设置为FALSE。
3) 获取相应视图控制器中 NSUserDefault 变量的值。如果值为 FALSE,则调用更新方法。
4) 现在,当应用程序变为活动状态时,将变量值设置为 TRUE。
5)在各自的视图控制器中获取它的值。如果值为 TRUE,则不调用更新方法。
6) 所以,update方法只会被调用一次。
希望对您有所帮助。可能会有一些细微的变化。
如有任何困难请告诉我。
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.
好吧,我从你的问题中了解到,你只想在通过单击 iPhone 上的应用程序图标启动应用程序时更新表数据。首先了解应用程序的执行,首先调用主函数,然后在你的应用程序中调用委托,
您可以在此方法中发布通知,因为它被调用一次。
而且,如果您想在 ViewDidLoad 方法中以自己的方式执行此操作,那么我认为您还必须删除通知的观察者,我认为这可能是它被调用两次的原因,因为您没有删除通知。
Well what i understand from your question that you only want to update the table data when you start your application by clicking the app icon on the iPhone.Well First understand the execution of the app, the main function is called first and then in your app delegate
you can post the notification in this method as its called once.
And more over if you want to do this by your own way in the ViewDidLoad method, then i think you must also remove the observer of notification,i think that may be the reason that its being called twice,because you are not removing notification.
作为编写这个问题和审查答案的一部分,我想我可能有一个很好的解决方案...
代码:
看起来工作正常...
As part of writing this question and reviewing answers I think I've got probably a good solution...
Code:
Seems to work ok...