这是我的搜索功能:
- (void)searchingMethod:(NSString *)aText{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"word LIKE %@",
[searchBar.text lowercaseString]];
[fetchRequest setPredicate:predicate];
//fetching array
NSLog(@"Place 1");
NSArray *wordArray = [context
executeFetchRequest:fetchRequest
error:nil];
NSLog(@"Place 2");
[self performSelectorOnMainThread:@selector(refreshTableView:)
withObject:wordArray waitUntilDone:NO];
NSLog(@"Searching End");
}
我用这个方法调用搜索函数:
- (void)searchBar:(UISearchBar *) searchBar textDidChange:(NSString *)Tosearch{
if([[searchBar text] length] >0)
{
NSThread *aThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(searchingMethod:)
object:searchBar.text];
[aThread start];
}
else
{
//others...
}
return;
}
通常,我会得到这样的结果:</p>
2012-07-05 00:04:46.706 MyApp[2376:207] Place 1
2012-07-05 00:04:46.783 MyApp[2376:207] Place 2
2012-07-05 00:04:46.823 MyApp[2376:207] searching End
搜索方法执行了十几遍后,就停在了这个位置。
2012-07-05 00:11:42.174 MyApp[2376:207] Place 1
继续搜索几次,就恢复正常了。然后又奇怪了……这让我很困惑。
我花了很多天尝试不同的多线程方法,结果还是一样。
请帮帮我!谢谢!