How do I programmatically cancel a search that uses a searchBar and fetchedResultsController?
I have a working iPad app, that uses Core Data, a SplitView, and uses the Master-Detail pattern. Think of it like the Apple messaging app, where you have a list of conversations in the root view controller, and when you select a conversation, all its messages appear in the DetailViewController.
I use a searchBar in the RootViewController, and since I use the UISearchDisplayDelegate and UISearchBarDelegate protocols, I use handleSearchForTerm to update my search results as the user types each character of their search words.
All this works really well.
Here is my problem. There is a thread that fetches data from the internet, and records can be added or removed from Core Data, while my search results are being displayed.
Before you have to ask, the thread does all its adds and deletes on the main UI thread, calling methods with performSelectorOnMainThread. So the search, adds and deletes occur in the same managedObjectContext.
So what happens is that I do a search, and results appear. Meanwhile, an update occurs, and one of the records in the result set gets deleted. Now, there's a mismatch b开发者_C百科etween the results and their indexPath.
My solution would be simple: When I detect that a delete occurs with
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
I want to programmatically cancel the search in the searchBar.
So how do I programmatically cancel a search? If the user clicks "Cancel" in the search bar, everything is fine. How can I programmatically do the same thing?
Any help is appreciated.
Peter
If you want to cancel the search, why not go ahead and use the following UISearchBarDelegate call
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;
This is assuming that you have the proper code in this location already.
精彩评论