I’m displaying a long list of items that I need to sort with 3 index (name, date, id). I then display the list with a UISegmentControl in self.navigationItem.titleView. The user can use that control to select which sort order he wants to display.
The user then have the option to filter that list based on some categories. I then have to sort again the list because it is possible that with the new filtered list, there will be only one name displayed so I don’t want to display the sort order by name. So I need to sort just to be able to reset the UISegmentControl.
Since the sort is taking a few seconds, I pushed it in another thread. Here is my code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"value"]) {
//The filter value has changed, so I have to redo the sort
[[self navigationItem] setTitleView:NULL];
[[self navigationItem] setTitle:@""];
self.navigationItem.rightBarButtonItem.enabled = false;
[self.tableView reloadData]; //I reload here because I want to display Loading... on my table (another var has been set before hands so the tableview will display that)
dispatch_async(backgroundQueue, ^(void) {
[self displayAfterFilterChanged];
});
}
}
-(void) displayAfterFilterChanged
{
displayState = DISPLAYRUNNING;
[self setupSort]; //<<< this is where the 3 sort index are setup based on the filter
dispatch_async(dispatch_get_main_queue(), ^(void) { //<<< I call all the UI stuff in the main_queue. (remember that this method has been call from dispatch_async)
[self.tableView reloadData]; //<< the table will display with the sorted and filtered data
self.navigationItem.rightBarButtonItem.enabled = true;
if (!self.leSelecteur) { //<<< if I don't have a UISegmentControl I display a title text
[[self navigationItem] setTitle:@"atitle"];
} else { // I have a UISegmentControl, so I use it.
[[self navigationItem] setTitleView:self.leSelecteur];
NSLog(@"before setneedsdisplay");
[self.navigationItem.titleView setNeedsDisplay];
NSLog(@"after setneedsdisplay");
}
});
}
NOW, THE PROBLEM:
The table is redisplaying itself right away, the rightbarbuttonitem is enabled right away.
But the navigationItem titleView takes 5-10 secondes before it display itself.
Looks like it is skipping a display cycle and catch the next one.
Is there a way I could speed it up ?
I can see that “before setNeedsdisplay” and “after setneedsdisplay” are displayed on the log right away. But the actual refresh occurs 5-10 sec later.
ok, got it. I was actually building my UISegmentControl in the thread. So I was playing with the UI not in the main thread (bad boy… slap, slap 😉 ). I moved all the code to create the control in the block called in the main thread, and voila, it worked. thank you all