Reputation: 1
i try to reload a tableview when the app enter in foreground. in the ViewController -> viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateTableViewData:) name: @"UpdateTableViewAccueil" object:nil];
in the ViewController the method used:
-(void)updateTableViewData:(NSNotification *) notification
{
[self readMsgsFromDB];
[self reloadTableMsgsReceived];
}
in appDelegate.m -> applicationWillEnterForeground
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateTableViewAccueil" object: nil];
after i launch the app from xcode, the first time when app enter in foreground the table is reloaded, but not the next times
Do you have some advices? Thanks in advance
Upvotes: 0
Views: 723
Reputation: 394
I faced a similar issue while trying to reloadData on receiving an NSNotification. It seems the notification is received in a thread different from the main thread. The below code, helped fix it.
Swift:
dispatch_async(dispatch_get_main_queue(), {self.tableView.reloadData()})
Replace self.tableView.reloadData() with the code you are using to reload the tableview.
Upvotes: 2
Reputation: 9687
On a side note, you do not have to post a custom notification to capture the fact that the application will enter the foreground. Try swapping this:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateTableViewData:) name: @"UpdateTableViewAccueil" object:nil];
With this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableViewData) name:UIApplicationDidBecomeActiveNotification object:nil];
If that works, you can remove the post notification you have.
Upvotes: 0