无法将控制权交还给 UI
我有一个应用程序,允许通过各种字段(名称、城市、id)搜索 sql 数据库,我尝试将搜索实现为队列中的 NSOperations。并且用户界面在第一次搜索之间更新,但由于某种原因,当操作在后台继续时,表格视图不会响应触摸。不确定我错过了什么,或者我是否需要操作队列的自定义运行循环,如果是这样,我将不胜感激,因为我对此不够熟悉,这是我现有的代码,
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *searchText = searchBar.text;
[self.searchQueue cancelAllOperations]; //NSOperationQueue
[self.searchResults startSearching:searchText];
[self searchID:searchText];
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchName:) object:searchText];
[self.searchQueue addOperation:operation1];
[operation1 release];
//NSLog(@"Add city search");
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchCity:) object:searchText];
[self.searchQueue addOperation:operation2];
[operation2 release];
}
所以发生的情况是 UI 一直保留到第一个搜索结束(没关系)并且刷新表视图。当每个操作完成时,它会刷新表格视图,但是在此期间,表格视图不会响应触摸......
任务本身看起来像这样......
-(void) searchCity:(NSString *)City{
searchObject *myCity = [[searchObject alloc] init];
self.searchResults.cityResults = [myCity searchCity:City]; //returns NSArray of items
if ([self.searchResults.cityResults count] == 0) {
[self.searchResults.cityResults addObject:[NSArray arrayWithObjects:@"Not Found.",@"",@"",@"",@"", nil]];
}
[self.searchResults refreshView];
//NSLog(@"Added from City Search\n%@",self.searchResults.cityResults);
}
I have an app that allows searches of an sql db by various fields (name, city, id) I have tried to implement the searches as NSOperations in a queue. and the ui is updated between the first search but for some reason the tableview will not respond to touches while the operations are continuing in the background. Not sure what i've missed or if i need a custom runloop for the operation queue if so would appreciate some help with that as i'm not familiar enough with that here's my existing code
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *searchText = searchBar.text;
[self.searchQueue cancelAllOperations]; //NSOperationQueue
[self.searchResults startSearching:searchText];
[self searchID:searchText];
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchName:) object:searchText];
[self.searchQueue addOperation:operation1];
[operation1 release];
//NSLog(@"Add city search");
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchCity:) object:searchText];
[self.searchQueue addOperation:operation2];
[operation2 release];
}
so what happens is the UI is held until the first search ends (that's ok) and it refreshes the table view. As as each operation completes it refreshes the tableview, however during this time the tableview will not respond to touches...
the task itself looks like this..
-(void) searchCity:(NSString *)City{
searchObject *myCity = [[searchObject alloc] init];
self.searchResults.cityResults = [myCity searchCity:City]; //returns NSArray of items
if ([self.searchResults.cityResults count] == 0) {
[self.searchResults.cityResults addObject:[NSArray arrayWithObjects:@"Not Found.",@"",@"",@"",@"", nil]];
}
[self.searchResults refreshView];
//NSLog(@"Added from City Search\n%@",self.searchResults.cityResults);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我回答了我自己的问题,但对于接下来的人来说......
这就像确保在主线程上执行表格视图刷新一样简单......
OK i answered my own question but for the next folks...
it was as simple as ensuring that the tableview refresh was performed on the main thread...