Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 是否在后台线程中通过数组执行搜索?_Objective C_Nsarray_Nspredicate - Fatal编程技术网

Objective c 是否在后台线程中通过数组执行搜索?

Objective c 是否在后台线程中通过数组执行搜索?,objective-c,nsarray,nspredicate,Objective C,Nsarray,Nspredicate,我从数据库peopleArray中获得了一个相当大的数组,它由我应用程序的所有用户组成。此数组用于搜索朋友。我的问题是,当用户开始在搜索栏中键入内容时,应用程序通常会在显示搜索到的用户之前冻结片刻 #pragma mark - SEARCH BAR - (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{ NSPredicate *predicate = [NSPredi

我从数据库
peopleArray
中获得了一个相当大的数组,它由我应用程序的所有用户组成。此数组用于搜索朋友。我的问题是,当用户开始在搜索栏中键入内容时,应用程序通常会在显示搜索到的用户之前冻结片刻

 #pragma mark - SEARCH BAR

- (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginsWith[cd] %@",@"Name", searchText ];
self.searchArray = [self.peopleArray filteredArrayUsingPredicate:predicate];
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContententForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
[self.tableView reloadData];
}

我希望在后台执行此操作,以便在加载时在tableView中放置一个
UIActivityIndicator
,但不确定在后台的何处或如何实现该方法。

首先,我建议使用计时器,这样就不会在每次按键时重新加载用户。我是这样做的:

//I put this in my private @interface
@property (nonatomic, strong) NSTimer *searchTimer;

//Then we have the method called on keypress
- (void)whateverMethodIsCalledOnKeypress {
    [self.searchTimer invalidate];
    self.searchTimer = nil;
    //put some logic that returns out of the function for empty strings etc. here
    self.searchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runSearch) userInfo:nil repeats:NO];
}

- (void)runSearch {
    //do whatever you need to run the search this way
    //it's only ever done at most once per second
    //so fast typists don't overload the processor
}
下面是一些执行异步过滤的代码

//Show your activity indicator
dispatch_async(dispatch_get_global_queue(0,0), ^{
    //call whatever you need to do on the filtering here
    dispatch_async(dispatch_get_main_queue(), ^{
        //Hide your activity indicator
        [self.tableView reloadData];
    });
});