6

我在我的應用程序中有一個UISearhBarController。它目前正在搜索在線數據庫,但我正在更改它來搜索核心數據庫。它目前正在使用此代碼:使用NSFetchedResultsController與UISearchBar

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if (self.billingSearchBar == searchBar) { 
     [self.billingSearchController filterResultsUsingString:searchText]; 
    } 
} 

- (void)filterResultsUsingString:(NSString *)filterString 
{ 
    self.billingSearchText = filterString; 
    NSArray *billing_codes = [self billingSearch:self.billingSearchText searchType:self.billingSearchCategory]; 
    self.displayedBillingSearch = billing_codes; 
    self.billingSearch = billing_codes; 
    [self.tableView reloadData]; 
} 

-(NSMutableArray*)billingSearch:(NSString*)searchString searchType:(NSString*)searchType 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/server/20111115/60b88126/billing_search/", [self getHost]]]; 

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; 
    [request setPostValue:searchString forKey:@"query"]; 
    [request setPostValue:searchType forKey:@"category"]; 

    [request startSynchronous]; 
    NSError *error = [request error]; 
    NSString *responseString; 
    if (!error) { 
     responseString = [request responseString]; 
    } 

    NSMutableArray *search_results = [[NSMutableArray alloc] init]; 
    NSDictionary *responseDictionary = [responseString JSONValue]; 

    for (id key in [responseDictionary valueForKey:@"search_results"]) { 
     [search_results addObject:key]; 
    } 
    return search_results; 
} 

所以我必須在覈心數據的數據庫設置好的,我只需要搜索/ NSFetchedResults控制器掛接到它。任何簡單的方法來做到這一點?

回答

6

最簡單的方法是使用兩個NSFetchedResultsControllers,並將「搜索」NSFRC上的謂詞設置爲UISearchBar中的searchText。在你決定使用哪個NSFRC之後,你只需設置你的self.fetchedResultsController =「搜索」NSFRC或「默認」NSFRC。

您也可以使用相同的NSFRC並更改謂詞,但這不是推薦的方式。

請參閱下面的註釋。如果所有這些變化都是謂詞,那麼一個FRC就可以了。

下面是示例代碼來完成這件事:

// First use the Searchbar delegate methods 
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [self filterContentForSearchText:searchText]; 
} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [self filterContentForSearchText:searchBar.text]; 
    [searchBar resignFirstResponder]; 
} 

// The method to change the predicate of the FRC 
- (void)filterContentForSearchText:(NSString*)searchText 
{ 
    NSString *query = searchText; 
    if (query && query.length) { 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ or department contains[cd] %@", query, query]; 
     [self.fetchedResultsController.fetchRequest setPredicate:predicate]; 
     [self.fetchedResultsController.fetchRequest setFetchLimit:100]; // Optional, but with large datasets - this helps speed lots 
    } 

    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Handle error 
     exit(-1); 
    } 

    [self.myTableView reloadData]; 
} 

這應該讓雅開始。

+2

我不同意。它在哪裏說改變謂詞「不是推薦的方法」?更容易更清晰的更新`NSFetchRequest`謂詞並執行新的獲取。有多個`NSFetchedResultsControllers`沒有意義。 – gschandler 2011-12-16 21:24:08

相關問題