I have a controller where the user searches for locations pulled from a Core Data store. To keep the UI responsive I fetch the object IDs on a background thread, like so:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self performSelectorInBackground:@selector(fetchLocationIDsForSearchTerm:)
withObject:searchText];
}
-(void) fetchLocationIDsForSearchTerm:(NSString *) searchTerm
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSManagedObjectContext *context = <a new context for this thread>
NSArray *locationIDs = <a CoreData query which returns object IDs>
[self performSelectorOnMainThread:@selector(didFetchLocationIDs:) withObject:locationIDs waitUntilDone:YES];
[context release];
开发者_开发问答 [pool release];
}
-(void) didFetchLocationIDs:(NSArray*) theLocationIDs
{
NSMutableArray *newLocations = [NSMutableArray array];
for (NSManagedObjectID *objectID in theLocationIDs)
{
[newLocations addObject:[[self managedObjectContext] objectWithID:objectID]];
}
self.locations = newLocations;
[[self mainTableView] reloadData];
}
...and then I pull the data out of self.locations in tableView:cellForRowAtIndexPath:
.
I would expect that the table would update when the results come back; but instead, it seems to update when the user enters the next letter of their search. Why isn't [[self mainTableView] reloadData] reloading the table?
UPDATE
I'm using a UISearchBar with SearchDisplayController. If I remove the SearchDisplayController, the problem goes away. Is there some property of the SearchDisplayController I should be using instead?
Because you have two tables.
Table 1 is created by you.
Table 2 is created by the search display controller and is displayed above your table view while a search is taking place. (Read the Overview section of the documentation carefully, it's tripped my up in the past with this very problem!)
Try changing your line to this :
[[[self searchDisplayController] searchResultsTableView] reloadData];
精彩评论