I have a simple app that downloads search results in XML when the user types in a UISearchBar
. The download+parsing is threaded and once done it fires an NSNo
reload your tableView
in viewWillLayoutSubviews
Call
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
instead of
[self.tableview reloadData]
I was able to get the same thing to work. But the issue was that the reload data needed to be called on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
I think this is more practical than the performSelectorOnMainThread option
My problem was that I was posting a NSNotification from a background thread. To avoid this, simply wrap your postNotificationMethod in a dispatch_async method like this:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedDownloading" object:result];
});
When this notification will be received, the tableView.reloadData
will be called on the main thread.
I have the same problem, and I have tried all the solution I can find on google. But All of them don't work.
At last I found that I add observer before viewDidLoad, and then [self.tableView reloadData] is not working.
First I call the setObserverForUserInfoDatabase in the viewDidLoad of my root navigation view controller. Because I think it was called earlier. The function is like this:
- (void)setObserverForUserInfoDatabase:(NSString *)name {
[[NSNotificationCenter defaultCenter] addObserverForName:name
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
[self loadUserInfo];
[self.tableView reloadData];
NSLog(@"User Panel Notification \n %@", self);
}];}
Then I move the code into viewDidLoad of the viewController itself.
- (void)viewDidLoad {
NSLog(@"View Did Load");
[super viewDidLoad];
[self setObserverForUserInfoDatabase:UserInfoDataBaseAvailabilityNotification];
}
And then everything works fine.
I don't know the reason yet. If anyone knows please tell me.