CroiOS CroiOS - 6 months ago 21
Objective-C Question

How to reload TableView data from AppDelegate.m?

I've tryed to reload TableView data from AppDelegate.m but with no success.

Here is code (whis is working in HomeViewController):


- (void)applicationWillEnterForeground:(UIApplication *)application
[self refreshTableData];


UINavigationController *navController = [[self.tabBarController viewControllers] objectAtIndex:0];
HomeViewController *home = [[navController viewControllers] objectAtIndex:0];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"MYTable" inManagedObjectContext:home.context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setFetchBatchSize:20];

[request setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"orderEnt" ascending:YES];

NSArray *newArray = [NSArray arrayWithObject:sort];

[request setSortDescriptors:newArray];

NSError *error;

NSMutableArray *results = [[home.context executeFetchRequest:request error:&error] mutableCopy];

[home setArr:results];

//NSLog(@"%@", home.arr);//here is showed correct data

home.context = self.managedObjectContext;

[home.myTableView reloadData];


Does anybody know how to reload data from AppDelegate.m?


You could implement

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewData) name:@"ReloadAppDelegateTable" object:nil]; 

in your viewDidLoad() method and

- (void)reloadTableViewData{
  [self.myTableView reloadData];

in the class where you want to reload the Data.

Then you send a Notification like this

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil];

whenever you want to reload the data.

Note after some years of iOS development experience: While this is a very easy and comfortable solution, it is not 100% reliable. Notifications can be delayed by several seconds under certain circumstances. I would highly recommend to create a fast and reliable delegate connection to your UITableViewController.