2014-09-27 71 views
0

在我的应用程序中,我使用带有可搜索的集合视图来让用户找到存储在coredata中的人员记录。我有搜索设置在每个字符输入时运行。起初,这个工作很好,但现在我有大量的数据,用户界面是滞后的。我想我需要使用另一个线程来做到这一点,但我是多线程的全神贯注。iOS更快地搜索集合视图

这里是我的代码:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    self.fetchedResultsController = nil; 

    NSError *error = nil; 
    if (![[self fetchedResultsController]performFetch:&error]) { 
     abort(); 
    } 

    [self.collectionView reloadData]; 
    [self.searchBar becomeFirstResponder]; 
} 

有没有办法做到这速度吗?

编辑:我应该指出的是,搜索字符串被送入他在获取谓语...

回答

1

我已使用THIS从数据库的UITABLEVIEW的方法,它的工作就像魅力。 你是如何实现的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

您能不能告诉我们高度的代码以及,

一些代码示例:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
self.feed.counter = 50; 

totalcustomers = [cust getTotalCustomer:@"textSearch" searchText:searchtextString custType:YES employeeId:@"" custId:[module getCurrentOrderCustomerId]]; 

     self.currentSearchTitle = searchText; 

     [cust setDatabasePath:DELEGATE.databasePath]; 

     self.feed.largeArray = (NSMutableArray *) [cust getSeachedCustomer:searchtextString custId:[module getCustomerId:[NSNumber numberWithInt:0]] action:@"textSearch"]; 

     self.availableCustomers = self.feed.largeArray; 

     [self.allCustomerTable reloadData]; 

     NSLog(@"%d ",totalcustomers); 
} 

和UITableviewScroll加载接下来的50条记录:

- (void)tableViewOverridedForScrollWithSearch:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSArray * newPosts; 

      newPosts = [self.feed newRowsFrom:indexPath.row andAction:@"textSearch" andSearchString:self.currentSearchTitle]; 
      NSUInteger newCount = [newPosts count]; 



      if (newCount) { 

       [self.availableCustomers addObjectsFromArray:newPosts]; 

       [self.allCustomerTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
       return; 
      } 
     } 

} 

and get next 50

- (NSArray *)newRowsFrom:(NSUInteger)newItem andAction:(NSString *)action andSearchString:(NSString *)searchString{ 

NSLog(@"Counter 1 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter); 

NSArray *result = [self newRowsFromDatabase:self.counter andAction:action andSearchString:searchString]; 

self.counter = self.counter + 50; 
NSLog(@"Counter 2 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter); 
return result; 

}

希望它会帮助你 感谢

+0

我没有得到这将如何加快从UISearchBar搜索... 我的cellForItemAtIndexPath是非常标准的。创建一个单元格,从fetchedresults控制器获取实体对象,设置单元格属性...我没有代码高度... – BBruce 2014-09-27 16:43:01

+0

如果您看到有两件事涉及:1.搜索和2.loareload数据集合。因此,如果数据较少,则工作正常。因此,如果用户输入“a”,则只需获得前50个数据,更新数据源并在用户滚动时重新加载collectionview.now,然后加载下一个50数据等等,我已经在我的应用程序中使用通用引擎从搜索,索引,滚动到下面实现了这样的功能,该引擎从数据中获取(搜索数据/简单的没有搜索数据,索引数据)base.i将在一段时间内显示代码片段 – Alok 2014-09-27 17:19:34

1

一次提取所有结果,并将它们保存在一个数组。然后在该数组中搜索,而不是每次调用该方法时从核心数据中获取数据。

+0

感谢。我从来没有将一个集合视图或表视图连接到一个数组。我会研究它。 – BBruce 2014-09-27 16:38:41