2011-08-12 20 views
0

我有4個CoreData數據庫。每個實際上都有自己的價值。但它太重了,我想把它減少到1數據庫。所以每次我想從數據庫中提取信息時,我都可以選擇要提取的內容。我需要使用NSPredicate來設置我想要拉izzit的字符串嗎?從數據庫中提取特定的謂詞

我是否設置了我的NSPredicate?

NSString *value = @"Food"; 
NSString *wildcardedString = [NSString stringWithFormat:@"%@*", value]; 
[[NSPredicate predicateWithFormat:@"ANY places.type like %@", wildcardedString]; 

以及如何將NSPredicate與獲取請求序列綁定?

這是我fetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController { 
    if (fetchedResultsController != nil) 
    { 
     return fetchedResultsController; 
    } 

    CoreDataMelakaAppDelegate *appDelegate = (CoreDataMelakaAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"WhereTo" inManagedObjectContext:context]]; 
    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]; 
    NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil] autorelease]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    // NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", wher.name]; 
    //[fetchRequest setPredicate:pred]; 
    NSString *value = @"Nasi"; 
    NSString *wildcardedString = [NSString stringWithFormat:@"%@*", value]; 
    [[NSPredicate predicateWithFormat:@"ANY wher.name like %@", wildcardedString]; 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Root"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 
    [aFetchedResultsController release]; 
    return fetchedResultsController; 
} 

回答

1

當你改變一個獲取結果控制器(FRC)使用的讀取請求的斷言,你必須創建一個新的獲取結果控制器。

你的代碼是正確的邏輯,但它僅與編譯爲一個謂語創建FRC一次:

ANY wher.name like Nasi* 

...每個FRC執行其提取時間,它使用的是精確的斷言。

如果您想使用靈活的謂詞,則每次更改謂詞時都需要創建一個新的FRC。

+0

表示即時調用一次?我真的不明白。是否有任何閱讀資料,你可以建議我通過? – Harvin

+0

您配置一個提取請求,並且每次調用'performFetch:'時,都會一遍又一遍地運行完全相同的提取請求。如果你想使用不同的謂詞,你必須初始化一個新的NSFetchedResultsController – TechZen

+0

好的。我知道了。我現在面臨着一個致命的錯誤,但即時通訊工作,將發佈另一個問題,如果我沒有設法解決它 – Harvin