Search code examples
iosobjective-cuitableviewsearchbardispatch-async

iOS UISearchController: Wait until UISearchController is dismissed after canceling search


I have a UISearchController that is dismissed when the user clicks the cancel button. After the user clicks the cancel button, I want the UISearchController to be dismissed first then the showNewTableData method needs to be called. Here is the code that I am using that works.

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showNewTableData];
    });
}

- (void)showNewTableData {
      if (self.searchController.active && self.searchController.searchBar.text.length > 0) {
         // show search data
      } else {
         // show non search data
      }
    }

Using dispatch_async seems to accomplish my requirements very well, but not sure if this is a good idea. If I don't use dispatch_async, I will end up showing search data because the search bar hasn't finished clearing text and it is still active. Any suggestions are appreciated.


Solution

  • Seeing as how you're trying to do a UI change, its fine to do this in the main thread.