2012-01-18 102 views
0

很奇怪的結果時,結合非常混亂的結果與NSPredicate過濾使用NSPredicate

它應該匹配名字濾波基礎上實現代碼如下一個搜索框和NSFetchedResultsController回來......但是,如果我在任何有效輸入名字,我得到一些結果不正確。如果我輸入任何全名,如「邁克約翰遜」或「凱利邁克爾」,我總是得到相同的過濾結果「安吉洛史密斯」。

的的NSLog將顯示 「名稱中包含[CD] \」凱利邁克爾\「」

然而,在屏幕上顯示的過濾結果將只顯示安傑洛·史密斯?任何想法如何解決這個問題?

我一直在使用這一疊後的解決方案,如果你需要更多的細節我在做什麼 How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar

- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString 
{ 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 

NSPredicate *filterPredicate = nil; 

/* 
Set up the fetched results controller. 
*/ 
// Create the fetch request for the entity. 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
// Edit the entity name as appropriate. 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Staff" inManagedObjectContext:self.managedObjectContext]; 

[fetchRequest setEntity:entity]; 

NSMutableArray *predicateArray = [NSMutableArray array]; 

if(searchString.length) 
{ 
    NSLog(@"here searchString is %@", searchString); 

    // your search predicate(s) are added to this array 
    [predicateArray addObject:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchString]]; 
    // finally add the filter predicate for this view 

    NSLog(@"%@", predicateArray); 

    if(filterPredicate) 
    { 
     filterPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:filterPredicate, [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray], nil]]; 
    } 
    else 
    { 
     filterPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray]; 
    } 
} 


    [fetchRequest setPredicate:filterPredicate]; 
... 
+0

嗯,這似乎是返回結果的正確數目。即如果我輸入「Mar」,我會得到兩個結果。這是有道理的,因爲我有兩名名字中帶有「Mar」的員工。馬克和瑪麗。但是,名稱卻代表「Angelo」和「Colin」。因此,它正在篩選正確的員工數量,但沒有顯示他們的專有名稱?我需要一個reloadData嗎? – sayguh

回答

2

我不會創建一個新的FRC過濾,除非數據集被改變時, 。相反,只是改變了謂詞和做其他取(在自定義的方法稱爲像filterContentForSearchText)

例子:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [self filterContentForSearchText:searchText]; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    searchBar.text = @""; 
    NSPredicate *predicate = nil; 
    [self.fetchedResultsController.fetchRequest setPredicate:predicate]; 
    [self.fetchedResultsController.fetchRequest setFetchLimit:0]; // 0 is no limit 

    [searchBar resignFirstResponder]; 
    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // 
    } 
    [self.myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 

} 

- (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]; // I use 100 because of the large dataset I am working with (4500 records) 
    } 

    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 
    [self.myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
} 

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

想要一個完整的例子謝謝 – sayguh

+0

添加到原始答案的示例。霍勒,如果你有問題。 –

+1

最好的解決方案!因爲我花了2天的時間嘗試使用2個FRC解決搜索問題!謝謝! –